-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_store.py
77 lines (72 loc) · 2.98 KB
/
db_store.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
import sqlite3 as sql
import csv
class db_store:
def __init__(self):
self.conn = sql.connect("data.sqlite")
self.sql_create_customers_table = """
CREATE TABLE IF NOT EXISTS Customers (
c_id integer PRIMARY KEY,
name text,
phoneNumber text
); """
self.sql_create_vendors_table = """
CREATE TABLE IF NOT EXISTS Vendors (
v_id integer PRIMARY KEY,
name text,
phoneNumber text,
keyword text,
isLazy integer DEFAULT 0
); """
self.sql_create_orders_table = """
CREATE TABLE IF NOT EXISTS Orders (
o_id integer PRIMARY KEY,
v_id integer,
c_id integer,
request text,
is_accepted integer DEFAULT 0,
is_complete integer DEFAULT 0,
phoneNumber text,
FOREIGN KEY(v_id) REFERENCES Vendors,
FOREIGN KEY(c_id) REFERENCES Customers
); """
self.sql_create_messages_table = """
CREATE TABLE IF NOT EXISTS Messages (
m_id integer PRIMARY KEY,
o_id,
v_id integer,
c_id integer,
messageBody text,
phoneNumber text,
FOREIGN KEY(o_id) REFERENCES Orders,
FOREIGN KEY(v_id) REFERENCES Vendors,
FOREIGN KEY(c_id) REFERENCES Customers
); """
self.init_database()
def __del__(self):
self.conn.commit()
self.conn.close()
def init_database(self):
c = self.conn.cursor()
c.execute(self.sql_create_customers_table)
c.execute(self.sql_create_vendors_table)
c.execute(self.sql_create_orders_table)
c.execute(self.sql_create_messages_table)
def load_db(self):
c = self.conn.cursor()
with open('dataload/customers.csv', 'r') as f:
data = csv.DictReader(f)
dbReady = [(i['c_id'], i['name'], i['phoneNumber']) for i in data]
c.executemany("INSERT INTO Customers (c_id, name, phoneNumber) VALUES (?, ?, ?);", dbReady)
self.conn.commit()
with open('dataload/vendors.csv', 'r') as f:
data = csv.DictReader(f)
dbReady = [(i['v_id'], i['name'], i['phoneNumber'], i['keyword']) for i in data]
c.executemany("INSERT INTO Vendors (v_id, name, phoneNumber, keyword) VALUES (?, ?, ?, ?);", dbReady)
self.conn.commit()
c = self.conn.cursor()
with open('dataload/orders.csv', 'r') as f:
data = csv.DictReader(f)
dbReady = [(i['o_id'], i['v_id'], i['c_id'], i['request'],
i['is_accepted'], i['is_complete']) for i in data]
c.executemany("""INSERT INTO Orders (o_id, v_id, c_id, request, is_accepted, is_complete)
VALUES (?, ?, ?, ?, ?, ?);""", dbReady)