import pymysql
#创建对象
con = pymysql.connect(host = 'localhost',user = 'root',password = '123456',database='python_db',port = 3306)
# 创建游标对象,利用游标可以执行sql语句
cur = con.cursor()
# 编写创建表的sql
sql = '''
create table python_data (
id int primary key auto_increment not null,
name varchar(20) not null,
age int,
score float(3,1) not null
)
'''
try:
# 执行创建表的sql,execute()是sql的执行语句
cur.execute(sql)
print("创建表成功")
except Exception as e:
print(e)
print("创建失败")
finally:
# 关闭连接
con.close()
使用pymysql来创建表
最新推荐文章于 2024-04-17 17:01:41 发布