-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
45 lines (33 loc) · 1.21 KB
/
database.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
import psycopg2
import datetime
class DatabaseConnection:
#Initialize FeestDatabaseConnection object
#Creates a psycopg2 connection and cursor
def __init__(self, dbName, dbUser, dbHost, dbPassword):
#Concatenated string to connect to database
dbConnect = "dbname='" + dbName
dbConnect += "' user='"+ dbUser
dbConnect += "' host='" + dbHost
dbConnect += "' password='" + dbPassword + "'"
#Connecting to PostGresQL Database. Throws error if unable to connect
#Connection is done outside of handler as global variable state is sometimes saved between function executions
try:
self.conn = psycopg2.connect(dbConnect)
except:
#TODO throw error here
print("I am unable to connect to the database")
self.conn.rollback()
def dbExecuteReturnNone(self, queryString, inputs=None):
cur = self.conn.cursor()
cur.execute(queryString, inputs)
self.conn.commit()
def dbExecuteReturnOne(self, queryString, inputs=None):
cur = self.conn.cursor()
cur.execute(queryString, inputs)
self.conn.commit()
return cur.fetchone()
def dbExecuteReturnAll(self, queryString, inputs=None):
cur = self.conn.cursor()
cur.execute(queryString, inputs)
self.conn.commit()
return cur.fetchall()