0

I'm using MySQLdb and run into the following problem:

STMT="""INSERT INTO test_table VALUES (%s, %s, %s, %s, %s)"""
rows=[('Wed Apr 14 14:00:00 2010', 23L, -2.3, 4.41, 0.83923)]
conn.cursor().executemay(STMT, rows)

results in:

Traceback (most recent call last):
 File "run.py", line 122, in <module>
 File "C:\Python25\lib\site-packages\mysql_python-1.2.2.0002-py2.5-win32.egg\MySQLdb\cursors.py", line 276, in _do_query
 db.query(q)
_mysql_exceptions.OperationalError: (1136, "Column count doesn't match value count at row 1")

Any hints ?

asked Sep 28, 2010 at 9:34

2 Answers 2

2

Try to write all columns in your INSERT explicitly:

STMT = 'INSERT INTO test_table (col1, col2, col3, col4, col5) VALUES (%s, %s, %s, %s, %s)'
answered Sep 28, 2010 at 9:37
Sign up to request clarification or add additional context in comments.

2 Comments

+1 almost exactly what I wrote, even the fake column names are the same!
That did it. I had an autoincrement id which I did not set in my statement. Stupid. But thanks for the fast answer.
1

How many columns are there altogether in test_table? Probably not 5, judging from the error. Try running SHOW CREATE TABLE test_table to see how the table is defined.

It is a good idea to explicitly list the column names when inserting in case new columns are added. Try this instead:

INSERT INTO test_table (col1, col2, col3, col4, col5) VALUES (%s, %s, %s, %s, %s)

You should change col1, col2, etc. to your real column names.

answered Sep 28, 2010 at 9:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.