-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathquery_sql.py
More file actions
44 lines (33 loc) · 1.14 KB
/
query_sql.py
File metadata and controls
44 lines (33 loc) · 1.14 KB
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
import psycopg2
from feathr.utils._env_config_reader import EnvConfigReader
# script to query SQL database for debugging purpose
def show_table(cursor, table_name):
cursor.execute("select * from " + table_name + ";")
print(cursor.fetchall())
q = """
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = %s;
"""
cur = conn.cursor()
cur.execute(q, (table_name,)) # (table_name,) passed as tuple
print(cur.fetchall())
# Update connection string information
host = "featuremonitoring.postgres.database.azure.com"
dbname = "postgres"
user = "demo"
env_config = EnvConfigReader(config_path=None)
password = env_config.get_from_env_or_akv('SQL_TEST_PASSWORD')
sslmode = "require"
# Construct connection string
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
conn = psycopg2.connect(conn_string)
print("Connection established")
cursor = conn.cursor()
show_table(cursor, "f_int")
cursor.execute("select * from f_location_avg_fare;")
print(cursor.fetchall())
# Clean up
conn.commit()
cursor.close()
conn.close()