MySqlDb抛出操作数应该在insert ignore语句中包含1个列

时间:2014-03-02 23:13:44

标签: python mysql mysql-python executemany

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

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

在环顾SO时,我发现了很多这个错误的例子,但他们已经处理了删除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是否错误?或者在传递给parms之前缺少我需要清理的executemany列表的另一个方面?

2 个答案:

答案 0 :(得分:3)

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

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

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

还应该注意到我搞砸了我的提交线。它应该是db.commit()而不是cur.commit()

答案 1 :(得分:0)

我尝试通过pandas.DataFrame.to_sql将带有列列表的df保存到mysql时遇到了这个问题,这可以自动执行某些过程。我通常使用join将列表转换为编码字符串,而不是使用json.dumps()json包还可以使用json.loads()轻松将其全部加载回原始格式。

在原始示例的情况下,我会将parms转换为数据框

parms = pd.DataFrame(parms)
parms
                               0      1  \
0  mathematica.stackexchange.com  43248

                                                   2  \
0  How to plot “Normalized distance” ...

                                                   3  \
0  Problem: there are two particles whose equatio...

                                                   4           5  \
0  [plotting, equation-solving, differential-equa...  1393801095

                                                   6  \
0  http://mathematica.stackexchange.com/questions...

                                                   7          8            9
0  http://mathematica.stackexchange.com/users/127...  Lawerance  mathematica

然后将列的列转换为json字符串:

parms[4] = parms[4].apply(json.dumps)
parms

0  mathematica.stackexchange.com  43248

                                                   2  \
0  How to plot “Normalized distance” ...

                                                   3  \
0  Problem: there are two particles whose equatio...

                                                   4           5  \
0  ["plotting", "equation-solving", "differential...  1393801095

                                                   6  \
0  http://mathematica.stackexchange.com/questions...

                                                   7          8            9
0  http://mathematica.stackexchange.com/users/127...  Lawerance  mathematica

启动MySQL连接(使用上面提供的详细信息):

import sqlalchemy
call = 'mysql+mysqldb://myuser:mypass@localhost:3306/qs' 
engine = sqlalchemy.create_engine(call)

然后使用内置的pandas函数转储它:

parms.to_sql('questions', engine, if_exists='append', chunksize=5000)
相关问题