首页 > 解决方案 > 如何在 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

所以大家请这是我第一次遇到堆栈溢出,我希望能找到好的答案。:(

标签: pythonmysqlcentos

解决方案


阅读错误。你定义了游标变量吗?

假设您使用的是 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)

希望我能提供帮助,但如果不尝试更具体。也许发布你所有的代码?


推荐阅读