-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_table.py
More file actions
36 lines (27 loc) · 738 Bytes
/
create_table.py
File metadata and controls
36 lines (27 loc) · 738 Bytes
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
# Python implementation to create a table in MySQL
import mysql.connector
# connecting to the mysql server
db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)
# cursor object c
c = db.cursor()
# create statement for tblemployee
employeetbl_create = """CREATE TABLE `employee_db`.`tblemployee` (
`empid` INT NOT NULL AUTO_INCREMENT,
`empname` VARCHAR(45) NULL,
`department` VARCHAR(45) NULL,
`salary` INT NULL,
PRIMARY KEY (`empid`))"""
c.execute(employeetbl_create)
c = db.cursor()
# fetch tblemployee details in the database
c.execute("desc tblemployee")
# print the table details
for i in c:
print(i)
# finally closing the database connection
db.close()