-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_edit.py
100 lines (88 loc) · 3.46 KB
/
db_edit.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
89
90
91
92
93
94
95
96
97
98
99
100
import sqlite3 #enable control of an sqlite database
DB_FILE="smapify.db"
db = sqlite3.connect(DB_FILE, check_same_thread=False) #open if file exists, otherwise create
c = db.cursor() #facilitate db ops
#info is a list of fieldValues in order without primary key
def insert(tableName, info):
'''inserts data into certain table, taking info as a list of parameters'''
# collect Column Data Names in strings
# c.execute('PRAGMA TABLE_INFO({})'.format(tableName))
c.execute('PRAGMA TABLE_INFO({})'.format(tableName))
colNames = ''
i = 0
tbl = c.fetchall()
for cols in tbl:
if tableName == 'users':
if i != len(tbl) - 1:
colNames += "'" + cols[1] + "'"+ ','
i += 1
else:
if i == 0:
i += 1 # primary key will update itself
else:
colNames += "'" + cols[1] + "'"+ ','
colNames = colNames[:-1]
values = ''
for val in info:
values += "'" + str(val) + "'" + ","
values = values[:-1]
print("INSERT INTO {0}({1}) VALUES ({2})".format(tableName,
colNames ,
values ))
c.execute("INSERT INTO {0}({1}) VALUES ({2})".format(tableName,
colNames ,
values ))
db.commit()
def findInfo(tableName,filterValue,colToFilt, sortCol = None, notEqual = None, fetchOne = None, asSubstring= False):
'''returns entire record with specific value at specific column from specified db table'''
if notEqual:
boolEqual = '!'
else:
boolEqual = ''
if sortCol:
sortQuery = 'ORDER BY {}'.format(sortCol)
else:
sortQuery = ''
if asSubstring:
filterValue = '%' + filterValue + '%'
eq = 'LIKE'
else:
eq = '='
command = "SELECT * FROM '{0}' WHERE {1} {3}{4} '{2}'".format(tableName,colToFilt,filterValue, boolEqual, eq)
command += sortQuery
c.execute(command)
listInfo = []
if fetchOne:
info = c.fetchone()
else:
info = c.fetchall()
if info:
for col in info:
#print(col)-
listInfo.append(col)
return listInfo
def modify(tableName, colToMod, newVal, filterIndex, filterValue):
print(("UPDATE {0} SET {1}='{2}' WHERE {3}='{4}'").format(tableName, colToMod, newVal, filterIndex, filterValue))
c.execute(("UPDATE {0} SET {1}='{2}' WHERE {3}='{4}'").format(tableName, colToMod, newVal, filterIndex, filterValue))
db.commit()
def delete(tableName, filterIndex, filterValue):
print(("DELETE FROM {0} WHERE {1} = '{2}'").format(tableName, filterIndex, filterValue))
c.execute(("DELETE FROM {0} WHERE {1} = '{2}'").format(tableName, filterIndex, filterValue))
db.commit()
def findLast(table, col):
print(("SELECT * FROM {0} WHERE {1} = (SELECT MAX({2}) FROM {3});").format(table, col, col, table))
c.execute(("SELECT * FROM {0} WHERE {1} = (SELECT MAX({2}) FROM {3});").format(table, col, col, table))
db.commit()
def checkApos(string):
'''Fixes strings with apostrophers'''
i = -1
aposIndexes = []
while True:
i = string.find("'", i + 1)
if i == -1: break
aposIndexes.append(i)
j = 0
for index in aposIndexes:
string = string[:index +j ] + "'" + string[index+ j:]
j += 1
return string