-
Notifications
You must be signed in to change notification settings - Fork 7
/
screenLockDB.py
105 lines (68 loc) · 2.77 KB
/
screenLockDB.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
import json,csv,sys,os,psycopg2
create1= """CREATE TABLE {0} (timeStart INT, timeStop INT) ; """
insert1="INSERT INTO {0} (timeStart,timeStop) VALUES ({1},{2}) ;"
drop = "DROP TABLE {0};"
#-----------------------------------------------------------------
# This script inserts phonelock and phone screen on data into tables
# one table for each user. Phone screen ON/OFF data are in tables: 'uXXdark'
# Phone lock data are in tables: 'uXXlock'
def main(argv):
#connecting to database
try:
con = psycopg2.connect(database='dataset', user='tabrianos')
cur = con.cursor()
except psycopg2.DatabaseError as err:
print('Error %s' % err)
exit()
if sys.argv[1]=='-insert':
#-----------------!!!!!!!!!!!!!-------------------------
# PHONE SCREEN ON/OFF DATA
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/sensing/dark/'
for filename in os.listdir(directory):
#creating table for each user with name: 'uXX'
uid = (filename.split('_'))[1][0:3]+ 'dark'
createQ = create1.format(uid)
cur.execute(createQ)
#opening each csv file and extracting information
with open(os.path.join(directory,filename),'rb') as inCsv:
parsed = csv.DictReader(inCsv , delimiter = ',' , quotechar='"')
#inserting records to DB
for record in parsed:
insertQ = insert1.format(uid, record['start'], record['end'])
cur.execute(insertQ)
# ------------!!!!!!!!!!!!-----------------------------
# PHONE LOCK DATA
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/sensing/phonelock/'
for filename in os.listdir(directory):
#creating table for each user with name: 'uXX'
uid = (filename.split('_'))[1][0:3]+ 'lock'
createQ = create1.format(uid)
cur.execute(createQ)
#opening each csv file and extracting information
with open(os.path.join(directory,filename),'rb') as inCsv:
parsed = csv.DictReader(inCsv , delimiter = ',' , quotechar='"')
#inserting records to DB
for record in parsed:
insertQ = insert1.format(uid, record['start'], record['end'])
print(insertQ)
cur.execute(insertQ)
con.commit()
con.close()
#do stuff
# -drop argument destroys all tables with phonelock and screen on/off data
elif sys.argv[1]=='-drop':
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/sensing/dark'
for filename in os.listdir(directory):
uid=(filename.split('_'))[1][0:3] +'dark'
dropQ=drop.format(uid)
cur.execute(dropQ)
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/sensing/phonelock'
for filename in os.listdir(directory):
uid=(filename.split('_'))[1][0:3] +'lock'
dropQ=drop.format(uid)
cur.execute(dropQ)
print('Tables destroyed successfully!')
con.commit()
con.close()
if __name__ == '__main__':
main(sys.argv[1:])