pymysql
import pymysql# 连接数据库 cursorclass = pymysql.cursors.DictCursor查询返回数据为dict类型connect = pymysql.connect(host = '127.0.0.1', port = 3306, user = 'user', password = 'password', db = 'test', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor)# 创建游标cursor = connect.cursor()# connect.commit() 提交变动,查询时不需要运行# 增sql = "INSERT INTO hello (name) VALUES ('ddd')"res = cursor.execute(sql)connect.commit()print(res)# 查sql = "SELECT * FROM hello WHERE id IN (2, 4)"cursor.execute(sql)res = cursor.fetchall() # 查询多条数据,返回列表# res = cursor.fetchone() # 查询单条数据print(res)# 改sql = "UPDATE hello SET name = 'aaa' WHERE id = 2"res = cursor.execute(sql)connect.commit()print(res)# 删sql = "DELETE FROM hello where id = 4"res = cursor.execute(sql)connect.commit()print(res)# 关闭连接connect.close()