带Python的Mysql不能插入带autoincrement id的记录,为什么?

2024-04-26 11:05:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在mysql数据库中创建了一个表'test',它有两列:(age int,name TEXT)。

然后,我使用以下代码插入记录(带列表):

record = [12, 'Tom']
cursor.execute("insert into test values(%s,%s)", record)

上面的代码在mysql中工作(我使用python 2.7进行编程)。

然后,我删除旧表,并希望通过向CREATE table sql添加以下代码,为新表添加一个AUTO_INCREMENT p_id字段:

P_id int(11) PRIMARY KEY AUTO_INCREMENT,

新的表可以工作,我在mysql中找到了它。 但是,当我尝试使用相同的代码插入新记录时:

record = [12, 'Tom']
cursor.execute("insert into test values(%s,%s)", record)

但它不起作用并报告:

OperationalError: (1136, "Column count doesn't match value count at row 1")

看来我应该自己加上你的身份证? 但它是否应该自动增加而我可以省略? 我在mysql方面是个新手,请帮助我了解细节。

这是我第一次在StackOverflow上提问,谢谢你的帮助。


Tags: 代码testidautoexecute记录mysqlrecord