executemany问题,MySQLdb的
问题描述:
我使用MySQLdb的和遇到以下问题:executemany问题,MySQLdb的
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)
结果:
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")
任何提示?
答
试着写的所有列在INSERT明确:
STMT = 'INSERT INTO test_table (col1, col2, col3, col4, col5) VALUES (%s, %s, %s, %s, %s)'
答
test_table*有多少列?可能不是5,从错误判断。尝试运行SHOW CREATE TABLE test_table
以查看表格是如何定义的。
当插入新列时,显式列出列名是个好主意。试试这个:
INSERT INTO test_table (col1, col2, col3, col4, col5) VALUES (%s, %s, %s, %s, %s)
您应该将col1,col2等更改为您的实际列名称。
+1几乎完全是我写的,甚至是假的列名都是一样的! – 2010-09-28 09:39:46
这样做。我有一个自动增量ID,我没有在我的声明中设置。笨。但是,谢谢你的快速回答。 – rocksportrocker 2010-09-28 10:38:41