-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlhelpers.py
88 lines (72 loc) · 2.97 KB
/
sqlhelpers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from app import mysql, session
class Table():
def __init__(self, table_name, *args):
self.table = table_name
self.columns = "(%s)" %",".join(args)
self.columnsList = args
#if table does not already exist, create it.
if isnewtable(table_name):
create_data = ""
for column in self.columnsList:
create_data += "%s varchar(100)," %column
cur = mysql.connection.cursor() #create the table
cur.execute("CREATE TABLE %s(%s)" %(self.table, create_data[:len(create_data)-1]))
cur.close()
#get all the values from the table
def getall(self):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM %s" %self.table)
data = cur.fetchall(); return data
#get one value from the table based on a column's data
#EXAMPLE using blockchain: ...getone("hash","00003f73gh93...")
def getone(self, search, value):
data = {}; cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM %s WHERE %s = \"%s\"" %(self.table, search, value))
if result > 0: data = cur.fetchone()
cur.close(); return data
#delete a value from the table based on column's data
def deleteone(self, search, value):
cur = mysql.connection.cursor()
cur.execute("DELETE from %s where %s = \"%s\"" %(self.table, search, value))
mysql.connection.commit(); cur.close()
#delete all values from the table.
def deleteall(self):
self.drop() #remove table and recreate
self.__init__(self.table, *self.columnsList)
#remove table from mysql
def drop(self):
cur = mysql.connection.cursor()
cur.execute("DROP TABLE %s" %self.table)
cur.close()
#insert values into the table
def insert(self, *args):
data = ""
for arg in args: #convert data into string mysql format
data += "\"%s\"," %(arg)
cur = mysql.connection.cursor()
cur.execute("INSERT INTO %s%s VALUES(%s)" %(self.table, self.columns, data[:len(data)-1]))
mysql.connection.commit()
cur.close()
#execute mysql code from python
def sql_raw(execution):
cur = mysql.connection.cursor()
cur.execute(execution)
mysql.connection.commit()
cur.close()
#check if table already exists
def isnewtable(tableName):
cur = mysql.connection.cursor()
try: #attempt to get data from table
result = cur.execute("SELECT * from %s" %tableName)
cur.close()
except:
return True
else:
return False
#check if user already exists
def isnewuser(username):
#access the users table and get all values from column "username"
users = Table("users", "name", "email", "username", "password")
data = users.getall()
usernames = [user.get('username') for user in data]
return False if username in usernames else True