首先需要在pycharm中安装pymysql模块
步骤:
1.引入pymysql
import pymysql
2.配置并获得连接:
conn=pymysql.connect(kwargs){
host='localhost',
port=3306,
user='xxx',
database='xxx'
charset='utf8'
}
3.获得查询游标
cursor = conn.cursor()
4.执行SQL语句:
affected = ursor.execute(sql)
例:
affected = ursor.execute('insert student(name,sex)values('xiaoming',1);')
5.从游标中获得结果:
cursor.fetchall()
6.提交结果
conn.commit()
7.断开连接
conn.close()