python创建mysql表的方法:
1.打开cmd命令行,输入:“pip install -i https://mirrors.aliyun.com/pypi/simple/ pymysql”。
2.再打开pycharm编写代码:
# -*- coding = utf-8 -*-
# @time:2020/5/28/028 17:07
# Author:cyx
# @File:demo.py
# @Software:PyCharm
import pymysql
# 创建连接
con = pymysql.connect(host='localhost',user='root',password='123456',database='python_db',port=3366)
cur = con.cursor()
sql="""
create table t_student(
sno int primary key auto_increment,
sname varchar(30) not null,
age int(2),
score float(3,1)
)
"""
try:
# 执行创建表的sql
cur.execute(sql)
print("创建表成功")
except Exception as e:
print("创建表失败")
print(e)
finally:
# 关闭连接
con.close()