-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_database.py
70 lines (56 loc) · 2.31 KB
/
sql_database.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
import sqlite3
from sqlite3 import Error
import os
# creates a sqlite database with tables for events and conditions
def create_connection(db_file):
#create a database connection to a SQLite database
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def create_table(conn, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
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)
sql_create_conditions_table = """ CREATE TABLE IF NOT EXISTS Conditions (
"condID" integer UNIQUE,
"condName" text NOT NULL,
"condDesc" text,
"screenshot" BLOB,
PRIMARY KEY("condID")
); """
sql_create_events_table = """ CREATE TABLE IF NOT EXISTS Events (
"eventID" integer UNIQUE,
"eventName" text NOT NULL,
"eventDesc" text,
"preID" INTEGER NOT NULL,
"postID" INTEGER NOT NULL,
PRIMARY KEY("eventID"),
FOREIGN KEY("postID") REFERENCES "Conditions"("condID"),
FOREIGN KEY("preID") REFERENCES "Conditions"("condID")
); """
# create a database connection
conn = create_connection(database)
# create tables
if conn is not None:
# create conditions table
create_table(conn, sql_create_conditions_table)
# create events table
create_table(conn, sql_create_events_table)
else:
print("Error! cannot create the database connection.")
if __name__ == '__main__':
main()