-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.py
40 lines (39 loc) · 1.04 KB
/
query.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
import psycopg2
db_name = "irctcdb"
user = "postgres"
password = "pg23"
host = "localhost"
port = "5432"
try:
conn = psycopg2.connect(database=db_name, user=user, password=password, host=host, port=port)
# print("Connected successfully")
except Exception as e:
print(f"Database not connected: {e}")
exit()
# Set isolation level to AUTOCOMMIT
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
# Read the SQL commands from the file
with open("queries.sql", "r") as file:
sql_commands = file.readlines()
with open("queries.txt",'r')as file1:
sql_ques = file1.readlines()
print(sql_ques)
for x in sql_ques:
print(x[0:-1])
for x in range(len(sql_commands)):
print("\nQuestion:")
print("--------")
print(sql_ques[x][0:-1])
print("\nQuery:")
print("-----")
print(sql_commands[x][0:-1])
cursor.execute(sql_commands[x][0:-1])
print("\nResults:")
print("-------")
val = cursor.fetchall()
for y in val:
print(y)
conn.commit()
cursor.close()
conn.close()