pymysql连接mysql数据库demo
# —— coding :utf-8 ——
# @time: 2020/9/11 17:04
# @IDE: xxxx
# @Author: xxxx
# @Email: xxxx@qq.com
# @File: handlemysql.py
# 导入pymysql模块
import pymysql
# 1.连接database
conn = pymysql.connect(
host="127.0.0.1",
port='3306',
user="root",
password="123456",
database="pymysql_test",
charset="utf8"
)
# 2.获取游标
cursor = conn.cursor()
# 3.定义要执行的SQL语句
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
# sql = """
# select * from students;
# """
# 4.执行SQL语句
cursor.execute(sql)
# # 5. 获取结果
res = cursor.fetchall()
# res = cursor.fetchone()
print(res)
# 6.关闭游标对象
cursor.close()
# 7.关闭数据库连接
conn.close()
https://blog.youkuaiyun.com/weixin_45912307/article/details/108538740