-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBclass.py
More file actions
executable file
·80 lines (69 loc) · 1.92 KB
/
DBclass.py
File metadata and controls
executable file
·80 lines (69 loc) · 1.92 KB
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
#
# DBclass based on SQLite, meant to be used in OscQLite, remote SQLite via OSC
# 15-09-2018: made it 3.5 compatible
# 27-10-2018: modified for OscQlite
# 28-10-2018: added exec() method
#
import csv, os, random, sqlite3, sys, time, datetime
from functools import partial
showinserts = 0
showupdates = 0
showselects = 0
showexecute = 1
class db(object):
def __init__( self, dbname = './config.sqlite' ):
self.DBFILE = os.path.join('', dbname)
print(self.DBFILE)
self.conn = sqlite3.connect(self.DBFILE, check_same_thread=False)
#self.conn.row_factory = sqlite3.Row # allows query results as dictionaries
self.cur = self.conn.cursor()
def insert(self, insq, tup):
if showinserts:print(insq, tup)
try:
self.cur.execute(insq, tup)
self.conn.commit()
#print self.cur.rowcount
except sqlite3.Error as e:
print("Error {}:".format(e.args[0]))
def update(self, upq):
if showupdates:print(upq)
try:
self.cur.execute(upq)
self.conn.commit()
#print self.cur.rowcount
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
def exec(self, exq):
if showexecute:print(exq)
try:
self.cur.execute(exq)
self.conn.commit()
return self.cur.rowcount
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
return "Error %s:" % e.args[0]
def select(self, selq):
if showselects: print(selq)
try:
self.cur.execute(selq)
rows = self.cur.fetchall()
return rows
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
return "Error %s:" % e.args[0]
def exists(self, selq): # returns true or false depending on query
self.cur.execute(selq)
rows = self.cur.fetchall()
if len(rows) == 0:
result = False
else:
result = True
return result
if __name__ == '__main__':
conf = db()
name = 'PCToos'
ip = conf.select('select IP from nodes where name="%s" ' % name)
print(ip[0][0])
rem = db('remote')
res = rem.select('select * from employees')
print(res)