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()