-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTables.py
76 lines (69 loc) · 2.41 KB
/
Tables.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
import mysql.connector
# Establish the database connection
mydb = mysql.connector.connect(host="localhost", user="root", passwd="")
mycursor = mydb.cursor()
# Create the database if it doesn't exist
mycursor.execute("CREATE DATABASE IF NOT EXISTS Library")
mydb.database = "Library" # Set the database context
# Create BookRecord table if it doesn't exist
mycursor.execute("SHOW TABLES LIKE 'BookRecord'")
if not mycursor.fetchone():
mycursor.execute(
"""CREATE TABLE BookRecord(
BookID varchar(10) PRIMARY KEY,
BookName varchar(35),
Author varchar(30),
Publisher varchar(30)
)"""
)
# Create UserRecord table if it doesn't exist
mycursor.execute("SHOW TABLES LIKE 'UserRecord'")
if not mycursor.fetchone():
mycursor.execute(
"""CREATE TABLE UserRecord(
UserID varchar(10) PRIMARY KEY,
UserName varchar(20),
Password varchar(20),
BookID varchar(10),
FOREIGN KEY (BookID) REFERENCES BookRecord(BookID)
)"""
)
# Insert data into UserRecord table
data1 = ("101", "Kunal", "1234", None)
data2 = ("102", "Vishal", "3050", None)
data3 = ("103", "Siddhesh", "5010", None)
query1 = "INSERT INTO UserRecord VALUES(%s, %s, %s, %s)"
mycursor.execute(query1, data1)
mycursor.execute(query1, data2)
mycursor.execute(query1, data3)
mydb.commit()
# Create AdminRecord table if it doesn't exist
mycursor.execute("SHOW TABLES LIKE 'AdminRecord'")
if not mycursor.fetchone():
mycursor.execute(
"""CREATE TABLE AdminRecord(
AdminID varchar(10) PRIMARY KEY,
Password varchar(20)
)"""
)
# Insert data into AdminRecord table
data4 = ("Kunal1020", "123")
data5 = ("Siddesh510", "786")
data6 = ("Vishal305", "675")
query2 = "INSERT INTO AdminRecord VALUES(%s, %s)"
mycursor.execute(query2, data4)
mycursor.execute(query2, data5)
mycursor.execute(query2, data6)
mydb.commit()
# Create Feedback table if it doesn't exist
mycursor.execute("SHOW TABLES LIKE 'Feedback'")
if not mycursor.fetchone():
mycursor.execute(
"""CREATE TABLE Feedback(
Feedback varchar(100) PRIMARY KEY,
Rating varchar(10)
)"""
)
# Close the cursor and database connection
mycursor.close()
mydb.close()