将字典附加到mysql表时出错

时间:2019-06-18 13:23:09

标签: python mysql

我正在查询mysql数据库中的一个表,并希望将结果追加到同一数据库中的另一个表中,但是出现了一个我似乎无法摆脱的错误。

到目前为止,我已经

  • 连接并在mysql数据库中执行sql查询
  • 将结果保存到熊猫数据框
  • 试图使用to_sql抛出模块错误,然后 其他一些错误...
  • 将下一个转换的数据帧转换为字典
  • 试图将字典追加到mysql表

代码:

import pymysql
import pandas as pd

dbServerName    = "..."
dbUser          = "..."
dbPassword      = "..."
dbName          = "..."

connObject = pymysql.connect(dbServerName, dbUser, dbPassword, dbName)
sqlQuery = """select id, uniqueUserId, time from searchResult where time>'2018-02-01' and time<'2018-03-2' limit 5"""
df = pd.read_sql(sqlQuery, connObject)
df['time'] = df['time'].astype(str)
# df.to_sql('test', con=conn, if_exists='append')
dfDict = df.to_dict(orient='list')

cursor = connObject.cursor()
table = 'test'
placeholders = ', '.join(['%s'] * len(dfDict))
columns = ', '.join(dfDict.keys())
sql = "INSERT INTO %s (%s) VALUES (%s)" % (table, columns, placeholders)
cursor.execute(sql, list(dfDict.values()))

我希望代码将字典添加到名为“ test”的表中。但是相反,我收到以下错误消息:

---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
<ipython-input-39-86395db9a7b9> in <module>
      4 columns = ', '.join(dfDict.keys())
      5 sql = "INSERT INTO %s (%s) VALUES (%s)" % (table, columns, placeholders)
----> 6 cursor.execute(sql, list(dfDict.values()))

InternalError: (1241, 'Operand should contain 1 column(s)')

dfDict内容看起来像这样:

{'id': [39457,
  39458,
  39459,
  39460,
  39461,
  ...],
  'time': ['2018-03-01 00:00:05',
  '2018-03-01 00:00:09',
  '2018-03-01 00:00:10',
  '2018-03-01 00:00:15',
  '2018-03-01 00:00:17',
  ...],
  'uniqueUserId': ['123abc-x123-y123-z123-1234567xyz',
  '123abc-x1415-y3264-z1343-13452xyz',
  '3413dgwe-x1143-124124-4214af-125wfag',
  'lk23h5l-l23k5h-2l3jk4-l15h-1po5j',
  'a987sfa-23kh4-n21m4nb1-1j5lkj2b3-kho7v62o',
  ...]}

其他信息:

  • Python 3.7.3
  • Mac OS Mojave 10.14
  • MySQL 5.6.33-0ubuntu0.14.04.1

关于我在做什么错的任何想法吗?

2 个答案:

答案 0 :(得分:2)

您的最后一行应该是:

cursor.executemany(sql, args)

其中args是这样计算的:

v = list(dfDict.values())
args = [[v[j][i] for j in range(len(v))] for i in range(len(v[0]))]

针对args的收益:

[[39457, '2018-03-01 00:00:05', '123abc-x123-y123-z123-1234567xyz'], [39458, '2018-03-01 00:00:09', '123abc-x1415-y3264-z1343-13452xyz'], ...

答案 1 :(得分:0)

如果您想一次将多个新元组放入一个关系中,我很确定您需要使用MySQLCursor.executemany()而不是MySQLCursor.execute()

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html

或者,您可以遍历该词典中的三个列表,并对每个数据集执行一个MySQLCursor.execute()