MySQLdb的:操作数应包含1列(S)(MySQLdb: Operand should conta

2019-09-30 05:53发布

我想一些数据插入到MySQL数据库,使用Python和MySQLdb的。 当我在我的程序执行以下功能,则MySQL返回错误“1241,‘操作数应包含1列(S)’”

用户名,密码和数据库是正确的,表中现有的所有权限授予。

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)')

Answer 1:

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



Answer 2:

我有当我产生这个错误SELECT与列的查询选择封闭的(错误)括号: SELECT (id, name, age) FROM members ;

需要注意的是,如果你有括号中所列只是一列不提出这个错误。



文章来源: MySQLdb: Operand should contain 1 column(s)