-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
65 lines (57 loc) · 1.38 KB
/
database.py
File metadata and controls
65 lines (57 loc) · 1.38 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import psycopg2
class _Db(object):
"""docstring for Db"""
def __init__(self, connexion_string):
super(_Db, self).__init__()
try:
self.connexion = psycopg2.connect(connexion_string)
#check if connexion succeeds
self.cursor = None#self.connexion.cursor()
self.connected = True
except:
self.connected = False
def select(self, sql_string, args = None, quantity = "one"):
try:
self.cursor = self.connexion.cursor()
self.cursor.execute(sql_string, args)
result = 0
if (quantity == "one"):
result = self.cursor.fetchone()
else:
result = self.cursor.fetchall()
self.cursor.close()
return (result)
except:
return -1
def insert(self, sql_string, args = None):
try:
self.cursor = self.connexion.cursor()
self.cursor.execute(sql_string, args)
result = self.cursor.rowcount
self.connexion.commit()
self.cursor.close()
return (result)
except:
return -1
def update(self, sql_string, args = None):
try:
self.cursor = self.connexion.cursor()
self.cursor.execute(sql_string, args)
result = self.cursor.rowcount
self.connexion.commit()
self.cursor.close()
return (result)
except:
return -1
def close(self):
self.connexion.close()
return (None)
DB = None
def NewConn(connexion_string):
global DB
DB = _Db(connexion_string)
#DB = "new connexion"
#print(DB)
"""def test():
global DB
print("TEST:", DB)"""