And*_*ndy 5 python mysql mysql-python executemany
在查看 Stack Exchange 提供的一些 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()
Run Code Online (Sandbox Code Playgroud)
难道是我用executemany错了?或者缺少parms我在传递到之前需要清理的列表的另一个方面executemany?
问题在于进入tags列的数据。它试图传递一个列表而不是一个字符串。
对于我原来问题中的示例,我使用此代码将其转换为字符串。
','.join([u'plotting', u'equation-solving', u'differential-equations', u'numerical-integration', u'notebooks'])
Run Code Online (Sandbox Code Playgroud)
还应该指出的是,我弄乱了我的提交行。应该db.commit()不是cur.commit()
| 归档时间: |
|
| 查看次数: |
10761 次 |
| 最近记录: |