python - 如何在 MYSQL 中创建一个使用 python 自动递增的 id 列?
问题描述
我在 CentOS 中使用 Python,我正在做一个简单的代码来将 python 链接到 MYSQL。我的问题是我想自动增加 id 列。我使用 CentOS 终端。
我试图写这个,所以程序会自动增加 id 列,因为我在一些答案中看到了它
record = [12,'tom']
cursor.execute("insert into t3(number,sent)values(%s,%s),record)
它给了我错误
<module> cursor.execute("insert into t3(number,sent)values(%s,%s)",record)
NameError :name 'cursor' is not defined
所以大家请这是我第一次遇到堆栈溢出,我希望能找到好的答案。:(
解决方案
阅读错误。你定义了游标变量吗?
假设您使用的是 mysql 模块,一旦您创建了数据库对象,您就可以使用它来创建游标。
import mysql.connector
database = mysql.connector.connect(host="localhost", user="username", passwd="password", database="nameofdatabase")
cursor = database.cursor()
record = [12,'tom']
cursor.execute("INSERT INTO t3(number,sent) VALUES(%s,%s)", record)
希望我能提供帮助,但如果不尝试更具体。也许发布你所有的代码?
推荐阅读
- python - Pyright/mypy:“expr”没有属性“id”
- php - 无法从 NSE 获取 JSON 数据
- amazon-web-services - 即使应用了已确认的角色,Amazon SNS 主题也不会触发 lambda
- excel - 插入新的表列以包含先前列的 IFELSE 结果
- python - 如何使用 bash 命令找出 docker deamon API URL
- ssis - Simba 连接器 - SSIS - BigQuery - 预览工作,但没有映射列
- c++ - 特征库中包含的薄 BDCSVD 的时间复杂度
- ionic-framework - Ionic Appflow/Bitbucket - 构建包含子存储库的应用程序时出错 - 无法加载子存储库
- sql - 将表变量传递给存储过程?
- javascript - Heroku TypeError:路径必须是绝对路径或指定根到 res.sendFile