-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.py
More file actions
30 lines (29 loc) · 734 Bytes
/
04.py
File metadata and controls
30 lines (29 loc) · 734 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
# coding=utf-8
def create_table():
#建立连接
db = pymysql.connect(host='localhost',user='root',password='root',db='MYDB')
#创建表
sql = '''
create table if no exit student (
id int not null AUTO_INCREMENT,
name text,
job text,
age int,
PRIMARY KEY('id')
)
'''
#使用cursor()方法创建一个游标对象
cursoe = db.cursor()
try:
cursor.excute(sql)
db.commit()
print('建表成功')
except BaseException as e:
db.rollback()
print(2)
finally:
#关闭游标连接
cursor.close()
db.close()
create_table()
print('数据库连接')