-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.py
More file actions
39 lines (33 loc) · 1.02 KB
/
Setup.py
File metadata and controls
39 lines (33 loc) · 1.02 KB
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
from mysql.connector import errorcode
import mysql.connector
from CarRenatlBackend import cursor
DB_NAME = "carrental"
def createDatabase():
query = f'create database if not exists {DB_NAME}'
cursor.execute(query)
print(f'Database {DB_NAME} created')
TABLES = {}
TABLES["todos"] = (
"create table `todos`("
"`id` int(11) not null auto_increment,"
"`todo` varchar(255) not null,"
"`created_on` datetime default current_timestamp,"
"primary key(`id`)"
")"
)
def createTables():
query = f'use {DB_NAME}'
cursor.execute(query)
for table_name in TABLES:
query_table = TABLES[table_name]
try:
print(f'creating table {table_name}')
cursor.execute(query_table)
print(f'created {table_name} table')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print(f'{table_name} already exists!')
else:
print(err.msg)
# createDatabase()
# createTables()