-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL.py
59 lines (46 loc) · 1.32 KB
/
SQL.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
import mysql.connector
class SQL():
def __init__(self):
#print("Database constructor")
self.conn = mysql.connector.connect(
user='election',
host='localhost',
password='',
database='')
self.cur = self.conn.cursor()
def __del__(self):
#print "Committing and closing DB connection\n"
self.conn.commit()
self.conn.close()
def insert_query(self, query, parameters = ''):
'''
Inserts a value into the database. Returns lastrowid
'''
self.cur.execute(query, parameters)
self.conn.commit()
return self.cur.lastrowid
def select_query(self, query, parameters = ''):
'''
Performs a select query, and returns a list of the results
'''
self.cur.execute(query, parameters)
return self.cur.fetchall()
def select_query_as_list(self, query, parameters = ''):
'''
If I have a list of stuff with only one field, enter the field as colName, and
then add it to a new list to return
'''
listy = []
for q in self.selectQuery(query, parameters):
listy.append(q[0])
return listy
def single_value_select_query(self, query, parameters = ''):
self.cur.execute(query, parameters)
try:
return self.cur.fetchone()[0]
except:
return None
def update_query(self, query, parameters = ''):
self.cur.execute(query, parameters)
self.conn.commit()
return self.cur.rowcount