-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_api.py
84 lines (71 loc) · 1.83 KB
/
sqlite_api.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
#!/usr/bin/python
# REF:
# https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.text_factory
# http://stackoverflow.com/questions/4429788/reading-back-a-datetime-in-sqlite3
# https://docs.python.org/2/library/sqlite3.html
import sys
import datetime
# sqlite3 DB support
##
try:
import sqlite3 as sqlite
except:
import sqlite as sqlite
if not('sqlite' in dir()):
print("WARNING: Could not find sqlite database support!")
sys.exit()
# CLASS FOR SQLITE
class sql:
# Open a database and supply a connection and cursor
##
def __init__(self, database):
#print(sqlite.PARSE_DECLTYPES)
self.con = sqlite.connect(database, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
# Use unicode for strange things, not regular ASCII
self.con.text_factory = sqlite.OptimizedUnicode
self.db = self.con.cursor()
self.id = None
self.data = []
self.count = None
# Auto close
##
def __del__(self):
self.con.commit()
self.con.close()
# Manual close
##
def close(self):
self.con.commit()
self.con.close()
# Query
##
def query(self, sql):
db = self.db
con = self.con
drv = 'sqlite'
db.execute(sql)
if drv == 'sqlite':
#self.last_count = db.rowcount
data = []
for r in [self.sqlite_dict_factory(db, x) for x in db.fetchall()]:
data.append(r)
self.data = tuple(data)
self.count = len(data)
try:
self.id = con.insert_id()
except:
self.id = db.lastrowid
# Run/execute a statement (UPDATE)
##
def run(self, sql):
db = self.db
self.data = ()
self.count = 0
db.execute(sql)
# Creates a dict of results for SQLite
##
def sqlite_dict_factory(self, cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d