- 连接数据库
- 创建游标对象
- 创建表
上代码:
# -*- coding:utf-8 -*-
import pymysql
# 创建连接
con=pymysql.connect(host='localhost',user='root',password='123456',database='ges_data',port=3306)
# 创建游标对象
cur=con.cursor()
# 创建表的sql语句
sql='''
create table T_student
(
sno int primary key auto_increment,
sanme varchar(30) not null,
age int(2),
score float(3,1)
)
'''
try:
cur.execute(sql)
print('创建表成功')
except Exception as e:
print(e)
print('创建失败')
finally:
con.close()
print('关闭连接')