-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.py
66 lines (50 loc) · 1.37 KB
/
connection.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import psycopg2
from config import config
usuario = ""
def funUsuario(valor):
global usuario
usuario = valor
def connect(query):
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
sqlquey = f"set my.app_user = '{usuario}'"
cur.execute(sqlquey)
try:
results = cur.fetchall()
except:
results = ""
conn.commit()
sqlquey = query
cur.execute(sqlquey)
try:
results = cur.fetchall()
except:
results = ""
conn.commit()
cur.close()
return results
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def column_names(query):
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
sqlquey = query
cur.execute(sqlquey)
column_names = [desc[0] for desc in cur.description]
conn.commit()
cur.close()
return column_names
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()