【问题标题】:How can I make an id column in MYSQL which has auto increment using python?如何在 MYSQL 中创建一个使用 python 自动递增的 id 列?
【发布时间】:2019-12-29 01:16:06
【问题描述】:

我在 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

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

【问题讨论】:

    标签: python mysql centos


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2015-02-26
      • 2011-02-18
      • 2014-10-01
      • 1970-01-01
      • 2011-09-25
      • 2016-02-19
      • 1970-01-01
      • 2021-11-23
      相关资源
      最近更新 更多