blob: 5a4e10c821a2561297a1bce664f31510f336086c (
plain)
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
|
import psycopg2
from psycopg2.extras import DictCursor
conn = psycopg2.connect(database="ET",
user="et_user",
password="Serenitas1",
host="debian",
cursor_factory=DictCursor)
def with_connection(f):
def with_connection_(*args, **kwargs):
# or use a pool, or a factory function...
try:
rv = f(conn, *args, **kwargs)
except Exception as e:
print(e)
conn.rollback()
else:
return rv
return with_connection_
@with_connection
def query_db(conn, sqlstr, params=None, one=True):
with conn.cursor() as c:
if params:
c.execute(sqlstr, params)
else:
c.execute(sqlstr)
conn.commit()
r = c.fetchone() if one else c.fetchall()
return r
|