首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySqlDb抛出操作数应包含插入忽略语句上的1列

MySqlDb抛出操作数应包含插入忽略语句上的1列
EN

Stack Overflow用户
提问于 2014-03-02 23:13:44
回答 2查看 9.1K关注 0票数 5

在查看堆栈exchange提供的一些websocket方法时,我希望将几个数据点保存到MySQL数据库中。然而,当我试图运行一个executemany命令时,我会得到以下错误:

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

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

下面是我的代码的一个简短、包含的示例:

代码语言:javascript
复制
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之前我需要清理它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-03 02:19:37

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

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

代码语言:javascript
复制
','.join([u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'])

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

票数 4
EN

Stack Overflow用户

发布于 2017-08-18 17:03:07

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

在最初的示例中,我将将parms转换为数据格式

代码语言:javascript
复制
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字符串:

代码语言:javascript
复制
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连接(使用上面提供的详细信息):

代码语言:javascript
复制
import sqlalchemy
call = 'mysql+mysqldb://myuser:mypass@localhost:3306/qs' 
engine = sqlalchemy.create_engine(call)

然后使用内置的熊猫功能将其转储:

代码语言:javascript
复制
parms.to_sql('questions', engine, if_exists='append', chunksize=5000)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22134987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档