-
Notifications
You must be signed in to change notification settings - Fork 4
/
DBManager.py
55 lines (35 loc) · 1.42 KB
/
DBManager.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
import sqlite3
def initDB(dbName):
#connector global variable
global conn
#cursor global varaible
global cursor
try:
# establish connection with the DB
conn = sqlite3.connect(dbName)
# conn = sqlite3.connect(":memory:")
# get cursor to manipulate the DB
cursor = conn.cursor()
# create a table (if it doesn't exist)
cursor.execute("""create table if not exists timeline
(timeStamp text, sourceType text,
data text) """)
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
def insertSingleRecord(data) :
# insert data, where data is a tuple ('timeStamp', 'sourceType', 'data')
# data should look like ('1488885118000', 'LocationHistory', 'latitudeE7 : 490737983, longitudeE7 : 174213983, accuracy : 20, altitude : 182'),
cursor.executemany("INSERT INTO timeline VALUES (?,?,?)", data )
# save data to database
conn.commit()
def insertMultipleRecords(data):
try:
# insert data, where data is a list of tuples [('timeStamp', 'sourceType', 'data')]
cursor.executemany("INSERT INTO timeline VALUES (?,?,?)", data)
# save data to database
conn.commit()
except sqlite3.Error as e:
print("Error %s:" % e.args[0])
def printTable():
for row in cursor.execute("SELECT * FROM timeline ORDER BY timeStamp DESC "):
print(row)