繁体   English   中英

MySqlDb引发Operand在插入忽略语句中应包含1列

[英]MySqlDb throws Operand should contain 1 column(s) on insert ignore statement

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

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

在四处查看时,我发现了许多此错误的示例,但它们涉及删除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列表的executemany

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

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

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

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

我在尝试通过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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM