-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_recorder.py
145 lines (124 loc) · 4.55 KB
/
sql_recorder.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sqlite3
from sqlite3 import Error
import os.path
from os import path
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def create_condition(conn, condition):
"""
Add a new condition into the condition table
:param conn:
:param condition:
:return: condition id
"""
sql = ''' INSERT INTO Conditions(condName, condDesc, screenshot)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, condition)
conn.commit()
return cur.lastrowid
def create_event(conn, event):
"""
Create a new event
:param conn:
:param event:
:return:
"""
sql = ''' INSERT INTO Events(eventName, eventDesc, preID, postID)
VALUES(?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, event)
conn.commit()
return cur.lastrowid
def convertToBinaryData(filename):
#Convert digital data to binary format
with open(filename, 'rb') as file:
blobData = file.read()
return blobData
def selectConditions(conn):
"""
Query all rows in the conditions table
:param conn: the connection object
"""
cur = conn.cursor()
cur.execute("SELECT * FROM Conditions")
return cur.fetchall()
def main():
# db_dir = input("enter directory for database:\n")
# db_name = (input("enter database name:\n")) + '.db'
# database = os.path.join(db_dir, db_name)
database = r'C:\Users\deand\OneDrive\Documents\sqlite\db\pythonsqlite.db'
# create a database connection
conn = create_connection(database)
with conn:
stop = False
print('to exit, press \"ENTER\"')
while(not stop):
usrInput = input("Insert a new Condition(type \"c\")? Insert a new Event(type \"e\")?\n").lower()
#create a new condition
if usrInput == 'c':
cN = input("Condition Name:\t")
cD = input("Condition Description:\t")
correctDir = False
print('to exit, press \"ENTER\"')
while(not correctDir):
scDir = input("screenshot directory:\t")
if scDir == '':
correctDir = True
scDir = None
elif path.exists(scDir):
correctDir = True
print("directory found!")
else:
print("sorry, that directory does not exist. try again")
try:
sc = convertToBinaryData(scDir)
condition = (cN, cD, sc);
condition_id = create_condition(conn, condition)
print('entry successfully inserted into the Conditions table under id ' + str(condition_id))
except:
print("Oops! there was an error inserting the data")
elif usrInput == 'e':
eN = input("Event Name:\t")
eD = input("Event Description:\t")
print("Conditions:")
for row in selectConditions(conn):
print(row[0:3])
isInt = False
while(not isInt):
try:
preID = int(input("select precondition ID:\t"))
if preID > len(row):
raise Exception()
isInt = True
except:
print("sorry, enter a valid id please")
isInt = False
while(not isInt):
try:
postID = int(input("select postcondition ID:\t"))
if postID > len(row):
raise Exception()
isInt = True
except:
print("sorry, enter a valid id please")
try:
event = (eN, eD, preID, postID);
event_id = create_event(conn, event)
print('entry succesfully inserted into the Events table under id ' + str(event_id))
except:
print("Oops! there was an error inserting the data")
elif usrInput == '':
stop = True
if __name__ == '__main__':
main()