安装 pymysql 三方依赖
pip install pymysql
from pymysql import Connection
conn = Connection(
host='localhost',
port=3306,
user='root',
password='root'
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db('test')
# 使用游标对象执行 SQL 语句
cursor.execute("""
CREATE TABLE `test_pymysql` (
`id` int NOT NULL COMMENT '主键',
`name` varchar(255) NOT NULL COMMENT '名字',
`age` int NOT NULL COMMENT '年龄',
`gender` varchar(255) NOT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT C