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/56539421/

相关文章:

MySQL分区删除需要时间

python - 将列表的每个元素作为函数的参数发送

python - 获取 "global name ' foo' is not defined"with Python's timeit

python - 在 CentOS 上使用 Python 脚本连接 MySQL

php - 将 MySQL 数据库转储包含约束表

mysql - 存储旧数据以加快访问速度的更好方法

php表单mysql更新保存数据

java - 什么是java中的数据源?有人可以用简单的语言向我解释吗?

database - 如何返回与 Solr 中的查询匹配的列..?

python - 模拟 python ctypes byref 参数 2 pre 2.6