-
Notifications
You must be signed in to change notification settings - Fork 6
/
database2.py
114 lines (89 loc) · 2.96 KB
/
database2.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
import sqlite3
class Model:
def __init__(self, db, table, fields, field_objects):
self.db = db
self.table = table
self.fields = fields
self.field_objects = field_objects
def create(self, values):
safe_values = []
for v in values:
safe_values.append(f"'{v}'")
sql = f"INSERT INTO {self.table} ({','.join(self.fields)}) VALUES( {','.join(safe_values)});"
self.db.cursor.execute(sql)
self.db.connection.commit()
def read(self, custom_sql=""):
self.db.cursor.execute(custom_sql if custom_sql else f"SELECT * FROM {self.table}")
return self.db.cursor.fetchall()
class MyDatabase:
def __init__(self, database_name='test.db'):
# connect to db get cursor
self.connection = sqlite3.connect(database_name)
self.cursor = self.connection.cursor()
def create_table(self, model: Model):
self.cursor.executescript(f"DROP TABLE IF EXISTS {model.table};")
field_text = []
for f in model.field_objects:
field_text.append(f"{f.name} {f.field_type}")
sql = f"CREATE TABLE {model.table}({','.join(field_text)});"
self.cursor.executescript(sql)
self.connection.commit()
class Field:
def __init__(self, name, field_type="TEXT", display_name=""):
self.name = name
self.field_type = field_type
self.display_name=display_name
def display_data(data):
output = []
for row in data:
print(row)
output.append(f"ID={row[0]}, NAME={row[1]}, PRICE={row[2]}.")
print("<br>\n".join(output))
db = MyDatabase('database_products.db')
products = Model(db, "products", ['ID', 'NAME', 'PRICE'], [
Field('ID', "INT"),
Field('NAME'),
Field('PRICE')
])
db.create_table(products)
products.create([1, 'Pencil', 123])
products.create([2, 'Paper', 456])
orders = Model(db, "orders", ['ID', 'FK_PRODUCT_ID', 'QTY'], [
Field('ID', "INT"),
Field('FK_PRODUCT_ID', "INT"),
Field('QTY')
])
db.create_table(orders)
orders.create([1, 1, 2])
orders.create([2, 2, 12])
data = products.read()
display_data(data)
print(orders.read())
print(orders.read(custom_sql="""
SELECT PRODUCTS.name, ORDERS.QTY, products.PRICE, ORDERS.QTY * products.PRICE AS ext
FROM ORDERS
LEFT JOIN PRODUCTS
ON orders.FK_PRODUCT_ID = products.ID;
"""))
print(orders.read(custom_sql="""
SELECT SUM(ORDERS.QTY * products.PRICE) AS total
FROM ORDERS
LEFT JOIN PRODUCTS
ON orders.FK_PRODUCT_ID = products.ID
"""))
db.connection.close()
"""
DBSQL HTTP/REST(METHODS/VERBS)
CREATE - INSERT - PUT
READ - SELECT - GET
UPDATE - UPDATE - POST
DELETE - DELETE - DELETE
SELECT PRODUCTS.name, ORDERS.QTY, products.PRICE, ORDERS.QTY * products.PRICE AS ext
FROM ORDERS
LEFT JOIN PRODUCTS
ON orders.FK_PRODUCT_ID = products.ID;
SELECT SUM(ORDERS.QTY * products.PRICE) AS total
FROM ORDERS
LEFT JOIN PRODUCTS
ON orders.FK_PRODUCT_ID = products.ID
"""