-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdb.py
123 lines (107 loc) · 3.94 KB
/
db.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sqlite3
import pdb
# TODO this should be objectified, and have the database_name set once for some factory object
def createTable(name, columns, database_name):
conn = sqlite3.connect(database_name)
c = conn.cursor()
columns = [scrub(column) for column in columns]
body = 'name text, ' + ', '.join([column + " text" for column in columns])
name = scrub(name)
# Create table
# http://stackoverflow.com/questions/3247183/variable-table-name-in-sqlite
sql = 'CREATE TABLE IF NOT EXISTS %s ( %s )' % (name, body)
#raise Exception(sql)
c.execute(sql) #[name,','.join(columns)]
conn.commit()
c.close()
def scrub(name):
return ''.join( chr for chr in name if chr.isalnum() or chr == '_' )
def scrubQuoted(name):
return ''.join( chr for chr in name if chr.isalnum() or chr == '_' or chr == ' ')
def dictValuePad(key):
return '"' + str(key) + '"'
def addEntity(name, hashtable, database_name):
conn = sqlite3.connect(database_name)
c = conn.cursor()
name = scrub(name)
# Insert a row of data
sql = "INSERT INTO %s (%s) VALUES (%s)"% (name, ','.join(hashtable.keys()),', '.join(['"'+value+'"'for value in hashtable.values()]))
#raise Exception(sql)
try:
c.execute(sql)
except sqlite3.OperationalError, e:
raise Exception(str(e.message) + ":" + sql)
conn.commit()
# We can also close the cursor if we are done with it
c.close()
def findTableContainingEntityWithIdentOrName(ident, database_name,flag=False):
conn = sqlite3.connect(database_name)
c = conn.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
results = c.fetchall()
#if flag: raise Exception(database_name)
for result in results:
result = result[0]
sql = "SELECT * FROM %s WHERE ident = '%s' OR name = '%s'" % (result,ident,ident)
# TODO would like case insensitive match here - not sure how to do that in sqlite
#raise Exception(sql)
#if flag:
# raise Exception(str(c.execute("PRAGMA table_info(courses)").fetchall()) + sql)
c.execute(sql)
fromThisTable = c.fetchone()
if fromThisTable:
c.close()
return (result,fromThisTable)
c.close()
return (None,None)
def grabColumnNames(table, database_name):
conn = sqlite3.connect(database_name)
c = conn.cursor()
sql = ("PRAGMA table_info(%s)"%scrub(table))
c.execute(sql)
results = c.fetchall()
return [result[1] for result in results]
def modifyTable(table, new_column, database_name):
conn = sqlite3.connect(database_name)
c = conn.cursor()
new_column = new_column.replace(' ','_')
sql = "ALTER TABLE %s ADD COLUMN %s TEXT" % (table, new_column.lower())
#raise Exception(sql)
c.execute(sql)
c.close()
conn.commit()
def grabEntity(name, ident, database_name):
conn = sqlite3.connect(database_name)
conn.row_factory = sqlite3.Row
c = conn.cursor()
name = scrub(name)
ident = scrubQuoted(ident)
sql = "SELECT * FROM %s WHERE ident = '%s'" % (name,ident)
#raise Exception(sql)
c.execute(sql)
result = c.fetchone()
c.close()
return result
def queryTable(table_name, hashquery, database_name):
conn = sqlite3.connect(database_name)
conn.row_factory = sqlite3.Row
c = conn.cursor()
name = scrub(table_name)
query = " AND ".join([ scrub(key) + " = '" + scrubQuoted(value) + "'" for key, value in hashquery.items()])
sql = "SELECT * FROM %s WHERE %s" % (name,query)
c.execute(sql)
result = c.fetchone()
c.close()
return result
def updateEntity(table, hashtable ,database_name):
conn = sqlite3.connect(database_name)
conn.row_factory = sqlite3.Row
c = conn.cursor()
table = scrub(table)
update = ', '.join([key.replace(' ','_')+" = '"+value+"'" for (key,value) in hashtable.items()])
# TODO should search that thing we are trying to update exists, or else we effectively fail silently ...
sql = "UPDATE %s SET %s WHERE ident = '%s' OR name = '%s'" % (table,update,hashtable["ident"],hashtable["ident"])
#raise Exception(sql)
c.execute(sql)
c.close()
conn.commit()