MySQLdb - OperationalError:(1136,"列数不匹配第1行和第34行的值计数;)

时间:2016-07-27 15:01:02

标签: mysql macos python-2.7 mysql-python

我在Macbook pro上运行以下脚本,所有脚本都正确执行并将数据插入数据库。我刚刚在我的工作iMac上安装了相同的python版本并安装了相同的模块,但新安装的iMac出错了。

以下是代码:

import MySQLdb
import traceback

databaseName = "dbName"
tableName = "tableName"
databaseLocation = "10.0.1.1"
databaseUsername = "uname"
databasePassword = "pass"

db = MySQLdb.connect(host=databaseLocation,
                    user=databaseUsername,
                    passwd=databasePassword,
                    db=databaseName,
                    use_unicode=1,
                    charset="utf8")

try:
    cur = db.cursor()
    cur.execute("SELECT count(*) as tot FROM %s" % tableName)
except (AttributeError, MySQLdb.OperationalError):
    cur = db.cursor()
    cur.execute("SELECT count(*) as tot FROM %s" % tableName)

insert = "insert into {} (title, vendorID, partnerName, packageFormat, packageType, jobStepID, status, percentage, platform, submittedBy, submitTime, portalResponce, totalFiles, completeTime) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)".format(tableName)
try:
    cur.execute(insert, ("test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test"))
    db.commit()

except Exception:
    print (traceback.format_exc())
    db.rollback()
cur.close()

以下是我在新安装上遇到的错误:

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1136, "Column count doesn't match value count at row 1")

这是它插入的表格:

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| JobID          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title          | varchar(255) | YES  |     | NULL    |                |
| vendorID       | varchar(255) | YES  |     | NULL    |                |
| partnerName    | varchar(255) | YES  |     | NULL    |                |
| packageFormat  | varchar(255) | YES  |     | NULL    |                |
| packageType    | varchar(255) | YES  |     | NULL    |                |
| jobStepID      | varchar(255) | YES  |     | NULL    |                |
| status         | varchar(255) | YES  |     | NULL    |                |
| percentage     | varchar(255) | YES  |     | NULL    |                |
| platform       | varchar(255) | YES  |     | NULL    |                |
| submittedBy    | varchar(255) | YES  |     | NULL    |                |
| submitTime     | varchar(255) | YES  |     | NULL    |                |
| portalResponce | longtext     | YES  |     | NULL    |                |
| totalFiles     | varchar(255) | YES  |     | NULL    |                |
| completeTime   | varchar(255) | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

据我所知,所有内容都匹配,非常奇怪,因为在一台Mac上脚本可以正常工作,而在新安装的iMac上则没有。有什么我完全可以忽略的吗?

0 个答案:

没有答案