MySqlDb抛出操作数应在插入忽略语句中包含1列 [英] MySqlDb throws Operand should contain 1 column(s) on insert ignore statement

查看:114
本文介绍了MySqlDb抛出操作数应在插入忽略语句中包含1列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While looking at some of the websocket methods that stack exchange offers, I wanted to save a few data points into a MySQL database. However, when I attempt to run an executemany command, I get the following error:

_mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)')

在四处查看时,我发现了许多此错误的示例,但它们已处理了删除SELECT语句上的括号的问题.我没有使用SELECT.我正在尝试INSERT.

While looking around SO, I found many examples of this error, but they have dealt with removing parenthesis on SELECT statements. I'm not using a SELECT. I'm attempting to INSERT.

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

A short, contained, example of my code looks like this:

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列表的另一方面?

Am I using the executemany incorrect? Or missing another aspect of the parms list that I need to clean before passing to executemany?

推荐答案

问题在于数据进入了tags列.它试图传递列表而不是字符串.

The problem was the data going into the tags column. It was attempting to pass a list instead of a string.

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

For the sample in my original question, I used this code to convert it to a string.

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

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

It should also be noted that I messed up my commit line. It should be db.commit() not cur.commit()

这篇关于MySqlDb抛出操作数应在插入忽略语句中包含1列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆