-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbconnector.py
106 lines (86 loc) · 3.03 KB
/
dbconnector.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
"""
Creating DB and storing the fetched license plate data.
Creates plate_fetch_db database and stores the data on platedata table.
"""
import logging
import mysql.connector
#Setting logging config
logging.basicConfig(level=logging.NOTSET)
class DB_INSTANCE:
def __init__(self, host:str, port:int, user:str, password:str) -> None:
self.host = host
self.port = port
self.user = user
self.password = password
#Connecting to DB-instance
def connectToInstance(self) -> None:
try:
DB = mysql.connector.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password
)
self.DB = DB
logging.info("Connected to DB!")
except Exception as e:
logging.error(e)
#Creating database
def createDB(self) -> None:
try:
db_name = "plate_fetch_db"
mycursor = self.DB.cursor()
creatdb = f"CREATE DATABASE {db_name}"
mycursor.execute(creatdb)
logging.info("DB created!")
self.DB.commit()
except Exception as e:
logging.error(e)
#Creating table
def createTable(self) -> None:
try:
mycursor = self.DB.cursor()
createtable = """
CREATE TABLE plate_fetch_db.platedata(
Plate_number VARCHAR(20),
Model VARCHAR(150),
Reg_year VARCHAR(20),
Engine_size VARCHAR(20),
seats VARCHAR(20),
Vehicle_ID VARCHAR(100),
Engine_number VARCHAR(100),
Fuel VARCHAR(30),
Reg_date VARCHAR(100),
Reg_location VARCHAR(100)
);
"""
mycursor.execute(createtable)
logging.info("Table created!")
self.DB.commit()
except Exception as e:
logging.error(e)
#Inserting data
def insertData(self, data:str) -> None:
try:
inmycursor = self.DB.cursor()
insertdata = """
INSERT INTO plate_fetch_db.platedata(
Plate_number,
Model,
Reg_year,
Engine_size,
seats,
Vehicle_ID,
Engine_number,
Fuel,
Reg_date,
Reg_location) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
"""
plate_details = [
tuple(data.split(" ")),
]
inmycursor.executemany(insertdata, plate_details)
logging.info("Data saved on DB!")
self.DB.commit()
except Exception as e:
logging.error(e)