-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
139 lines (126 loc) · 4.38 KB
/
storage.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
import sqlite3
class Storage():
def __init__(self, file='data.db'):
self.file=file
def __enter__(self):
self.connection = sqlite3.connect(self.file)
# self.connection.row_factory = sqlite3.Row
return self.connection.cursor()
def __exit__(self, type, value, traceback):
self.connection.commit()
self.connection.close()
class Operations():
def __init__(self):
self.cars_table = """CREATE TABLE IF NOT EXISTS cars(
Make text,
Model text,
Trim text,
Year text,
Price text,
Mileage integer,
Engine float,
Fuel text,
Transmission text,
Tax float,
Insurance text,
MPG integer,
KM integer,
Acceleration integer,
Link text,
Id integer primary key
)"""
self.price_table = """CREATE TABLE IF NOT EXISTS price_watch(
H_Date text,
H_Price real,
Id integer,
UNIQUE (H_Date, H_Price, Id),
FOREIGN KEY (Id) REFERENCES cars (Id)
);"""
self.insert_car = """INSERT INTO cars (
Make,
Model,
Trim,
Year,
Price,
Mileage,
Engine,
Fuel,
Transmission,
Tax,
Insurance,
MPG,
KM,
Acceleration,
Link,
Id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);"""
self.tasks = []
def create_tables(self):
with Storage() as cursor:
cursor.execute(self.cars_table)
cursor.execute(self.price_table)
def insert_tables(self):
with Storage() as cursor:
cursor.execute(self.insert_car)
class DB():
def some_db(self):
""" START. Adding a record into Database """
with Storage() as cursor:
try:
cursor.execute(self.insert_date,
(self.current_date, self.car_description['Price'], self.car_description['Id']))
cursor.execute(self.insert_car, list(self.car_description.values()))
except sqlite3.IntegrityError:
""" TODO: Search Results Web results SQLite IntegrityError: UNIQUE constraint failed:"""
""" Checkig a previous price and comparsion with a current one """
# row_check = f"SELECT Price from cars WHERE Id = {self.car_description['Id']}"
""" How to make a comparison and save a result. """
cursor.execute(self.row_check, (self.car_description['Id'],))
previous_price = cursor.fetchone()
print("Old Price: " + ''.join(previous_price))
print("Current Price: " + self.car_description['Price'])
price_difference = int(''.join(previous_price).replace('£', '')) - int(
self.car_description['Price'].replace('£', ''))
print("Price difference: " + str(price_difference))
if price_difference != 0:
cursor.execute(self.update_car, (self.car_description['Price'], self.car_description['Id']))
""" TODO: create a table for price traking """
cursor.execute(self.price_select, (self.car_description['Id'],))
result = cursor.fetchall()
""" To get a dictionary from the request """
data = dict(zip([c[0] for c in cursor.description], result[0]))
print(data)
print(type(result))
print(result)
""" END Database"""
if __name__ == '__main__':
with Storage() as cursor:
cars_table = """CREATE TABLE IF NOT EXISTS cars(
Make text,
Model text,
Trim text,
Year text,
Price text,
Mileage integer,
Engine float,
Fuel text,
Transmission text,
Tax float,
Insurance text,
MPG integer,
KM integer,
Acceleration integer,
Link text,
Id integer primary key
)"""
price_table = """CREATE TABLE IF NOT EXISTS price_watch(
H_Date text,
H_Price real,
Id integer,
UNIQUE (H_Date, H_Price, Id),
FOREIGN KEY (Id) REFERENCES cars (Id)
);"""
cursor.execute(cars_table)
cursor.execute(price_table)
#cursor.execute()