python - 如何在使用 Python 插入 MySQL 数据库后获取 "id"?

标签 python mysql database

我执行一个 INSERT INTO 语句

cursor.execute("INSERT INTO mytable(height) VALUES(%s)",(height))

我想得到主键。

我的表有 2 列:

id      primary, auto increment
height  this is the other column.

我刚刚插入后如何获得“id”?

最佳答案

使用 cursor.lastrowid 获取插入游标对象的最后一行 ID,或使用 connection.insert_id() 获取该连接上最后插入的 ID .

关于python - 如何在使用 Python 插入 MySQL 数据库后获取 "id"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2548493/

相关文章:

python - 类中的字段变量 (Python)

python - InvalidArgumentError : Input to reshape is a tensor with 88064 values, 但请求的形状需要 38912 的倍数 [[{{node Reshape_17}}]]

python - 有没有可能像return一样一次yield两个东西?

mysql - 电子商务数据库设计

mysql - Highcharts - 来自 MySQL 的预期格式的系列数据

sql - 查找每个 GROUP BY 结果中的行数

database - 使用 HeidiSql 访问数据库,如 PHPMyAdmin (localhost)

python - 在ansible中运行python函数

java - 访问 H2 DB 时出现表 "myTable"未发现错误

字典应用的数据库设计