-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_sqlite_db.py
67 lines (58 loc) · 1.75 KB
/
py_sqlite_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
#
# PROGRAM: PYTHON CONSOLE DATABASE BROWSER
# MODULE : SQLITE DATABASE CONNECTOR
#
import sqlite3
import traceback
import sys
from datetime import datetime
from pydbro.py_log import msg_log
connstr = "./db/001.db"
def set_conn(p_db_name):
global connstr
connstr = p_db_name
def get_conn():
global connstr
return connstr
def qry2dict(con, qry, qry_params=(), dbfile=None):
res = {}
try:
dt = str(datetime.now())
# msg_log(dt+" "+qry+"\n")
# if dbfile is None:
if con is None:
con = sqlite3.connect(get_conn())
cur = con.cursor()
cur.execute(qry, qry_params)
try:
data = cur.fetchall()
except:
con.commit()
return (con, res, cols)
cols = []
if cur.description is not None:
for col in cur.description:
cols.append(col[0])
# if dbfile is not None:
con.commit()
# else:
# con.close()
if len(data) > 0:
i = 0
for col in cols:
res[col] = []
for row in data:
res[col].append(row[i])
i += 1
return (con, res, cols)
except Exception as e:
# con.close()
# traceback.print_exc(file=sys.stdout)
# print ("ERR [ FAILED QRY DATABASE ] 001000x0010 (1) : Cannot Query Database")
tramsg = ""
exc_type, exc_value, exc_traceback = sys.exc_info()
traces = traceback.extract_tb(exc_traceback)
for frame_summary in traces:
tramsg += "{} {}".format(frame_summary.name, frame_summary.lineno)
msg_log("ERROR : " + str(e) + " " + qry + " " + tramsg)
pass