Python mysql cannot insert record with auto increment id, why?

I create a table 'test' with two columns: (age int, name TEXT) in the mysql database.

Then I insert the record using the following codes (with a list):

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

The above codes work in mysql (I use python 2.7 for programming).

Then I delete the old table and want to add the AUTO_INCREMENT P_id field for the new table by adding the following code to CREATE TABLE sql:

 P_id int(11) PRIMARY KEY AUTO_INCREMENT, 

And the new table works, and I find it in mysql. However, when I try to insert a new record using the same codes:

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

But this does not work and reports:

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

Looks like I should add the P_id value myself? But should it grow automatically, and can I skip this? I am new to mysql and please help with the details.

This is my first question on StackOverflow and thanks for any help.

+5
source share
1 answer

Use this query:

 insert into test (age,name) values(%s,%s) 

Your code will look like this:

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

Source: https://habr.com/ru/post/1208407/


All Articles