-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
47 lines (44 loc) · 1.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# accepts sql password and minimized sql query as args
# minimized being a qery without any comments or newline carriage returns
# main() parses any sql query and returns a list array for each row in result
# remove commented database criteria to establish connection
# normal sql network access limits/firewalls apply
# https://pynative.com/python-postgresql-select-data-from-table/
# imports the postgres results for all oclc numbers matching sql location code
# results = main('passwd', 'mul-min.sql')
def main(password,sqlName):
import psycopg2, sys
from python import utilities
from python.utilities import sqlImport
sqlName = sqlImport(sqlName)
try:
# uncomment connection() lines below replacing with your local Sierra credentials
# connection = psycopg2.connect(user = "user",
# password = password,
# host = "hostIP",
# port = "port",
# database = "db")
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute(sqlName)
# cursor.execute("SELECT * FROM sierra_view.item_record LIMIT 100;")
records = cursor.fetchall()
# print("You are connected to - ", record,"\n")
results = []
for row in records:
#print("Item", row, "\n")
results.append(row[0])
return results
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
if __name__ == "__main__":
# results = main(args,kwargs)
main()