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

我执行一个 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 3 中 x**4.0 比 x**4 快?

python - Windows Scipy 安装 : No Lapack/Blas Resourc

linux - 如何删除具有特定名称的文件夹

python - pandas 获取不在其他数据框中的行

linux - 遍历 ls -l 输出的每一行

linux - 如何在 Bash 中运行超时的进程?

linux - Mac 和 Linux 上的文本文件中的递归搜索和替换

python - 如何在python中声明零数组(或一定大小的数组)

python - 在 SQLAlchemy 中使用 OR

linux - 在 Linux 脚本中隐藏终端上的用户输入