gpt4 book ai didi

python - MySqlDb throws Operand should contain 1 columns(s) on insert ignore statement

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:01 37 4
gpt4 key购买 nike

在查看 stack exchange 提供的一些 websocket 方法时,我想将一些数据点保存到 MySQL 数据库中。但是,当我尝试运行 executemany 命令时,出现以下错误:

_mysql_exceptions.OperationalError: (1241, '操作数应包含 1 列')

当环顾四周时,我发现了很多这个错误的例子,但他们已经处理了删除 SELECT 语句上的括号。我没有使用 SELECT。我正在尝试 INSERT

我的代码的简短示例如下所示:

import MySQLdb as mdb
db = mdb.connect(host='localhost',user='myuser',db='qs',passwd='mypass',use_unicode=True,charset='utf8')
cur = db.cursor()
db_qry = """INSERT IGNORE INTO questions (id, site_base, title, body_sum, tags, last_act_dt, url, owner_url, owner, api_site) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""


parms = [(u'mathematica.stackexchange.com',
43248,
u'How to plot “Normalized distance” in this problem',
u"Problem: there are two particles whose equationsof motion both satisfy -n Abs[x[t]]^n/x[t] == x''[t]. But their initial conditions are different: one is x'[0] == 0, x[0] == 2;another is x'[0] == 0, ...",
[u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'],
1393801095,
u'http://mathematica.stackexchange.com/questions/43248/how-to-plot-normalized-distance-in-this-problem',
u'http://mathematica.stackexchange.com/users/12706/lawerance', u'Lawerance', u'mathematica')]

cur.executemany(db_qry, parms)
cur.commit()

我使用的 executemany 不正确吗?或者缺少传递给 executemany 之前我需要清理的 parms 列表的另一个方面?

最佳答案

问题是数据进入 tags 列。它试图传递一个列表而不是一个字符串。

对于我原来问题中的示例,我使用此代码将其转换为字符串。

','.join([u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'])

还应该注意,我弄乱了我的提交行。它应该是 db.commit() 而不是 cur.commit()

关于python - MySqlDb throws Operand should contain 1 columns(s) on insert ignore statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134987/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com