MySQLdb:操作数应包含1列

时间:2013-02-01 13:58:41

标签: python mysql mysql-python

我正在尝试使用Python和MySQLdb将一些数据插入到MySQL数据库中。当我在程序中执行以下函数时,MySQL返回错误“1241,'操作数应包含1列'”

用户,密码和数据库是正确的,表格是现有的,所有权利都被授予。

def write_to_mysql(pname, dat, like, reachs, talker, friendsfans):
''
    try:
        con = mdb.connect(user='user', passwd='password', host='localhost', db='database');
    except Exception. err:
        print(err)

    with con:

        cur = con.cursor()
        cur.execute("INSERT INTO fbinsights (page, datum, likes, reach, talking, fanfriends) VALUES( %s, %s, %s, %s, %s, %s)", (pname, dat, like, reachs, talker, friendsfans))

    connection.commit()

哪里出错?

完整追溯:

File "insights.py", line 111, in <module>
    main()
  File "insights.py", line 108, in main
    write_to_mysql(PAGE_NAME, date, likes_atm, reach_day, talking_day, friends_of_fans)
  File "insights.py", line 90, in write_to_mysql
    cur.execute("INSERT INTO fbinsights (page, datum, likes, reach, talking, fanfriends) VALUES( %s, %s, %s, %s, %s, %s)", (pname, dat, like, reachs, talker, friendsfans))
  File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-freebsd-9.0-RELEASE-p3-amd64.egg/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-freebsd-9.0-RELEASE-p3-amd64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')

2 个答案:

答案 0 :(得分:4)

@schlamar回答了这个问题。传递给MySQL的错误类型。

答案 1 :(得分:0)

当我使用列生成SELECT查询以在括号中选择括号(错误)时,我遇到此错误:SELECT (id, name, age) FROM members;

请注意,如果括号中只列出一列,则不会引发此错误。