pymysql 获取插入数据的主键id
import pymysql database = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='qiche' ) cursor = database.cursor() for i in range(5): cursor.execute('insert into test (name) values ("la")') print(database.insert_id()) database.commit() cursor.close() database.close() 通过db.insert_id()方法可以获取插入数据的主键id, 注意一定要在commit之前获取,否则返回0