时间:2022-12-06 01:08
python创建mysql表的方法:
1.打开cmd命令行,输入:“pip install -i https://mirrors.aliyun.com/pypi/simple/ pymysql”。
2.再打开pycharm编写代码:
#-*-coding=utf-8-*-
#@time:2020/5/28/02817:07
#Author:cyx
#@File:demo.py
#@Software:PyCharm
importpymysql
#创建连接
con=pymysql.connect(host='localhost',user='root',password='123456',database='python_db',port=3366)
cur=con.cursor()
sql="""
createtablet_student(
snointprimarykeyauto_increment,
snamevarchar(30)notnull,
ageint(2),
scorefloat(3,1)
)
"""
try:
#执行创建表的sql
cur.execute(sql)
print("创建表成功")
exceptExceptionase:
print("创建表失败")
print(e)
finally:
#关闭连接
con.close()