【问题标题】:Read from list into MySql从列表中读取到 MySql
【发布时间】:2019-01-18 08:36:18
【问题描述】:

这是我从一些数据读取到 MySql 的代码,Mysql 表包含 3 列,在其中一个列中我想插入一个数组的项,每一个都插入一个新行,我不想重复行:

db = MySQLdb.connect("127.0.0.1", "root", "M0", "my", local_infile=True,use_unicode=True, charset="utf8")
cursor = db.cursor()
r=["2","3"]
params = ['?' for item in r]
sql="insert ignore into array (firt,last_name,arrays) values 'nina','sa',(%s);" % ','.join(params)
cursor.execute(sql,r)
db.commit();
db.close()

我也已经看过[1]这个帖子了!

但是,在他们两个上我都收到了这个错误:

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting

有什么建议吗?

【问题讨论】:

  • 你试过打印你的参数吗?
  • 请打印您的查询(使用实际参数值)并显示出来。
  • @mad_ ,刚刚做到了!表明 ”?” !!!为什么会这样?
  • 您正在迭代 n 次并分配 '?'每一次。你真的不需要你的参数列表。更正并编辑帖子
  • @mad_ 我更正了,我没有为列表分配参数!但收到已发布的错误。

标签: python mysql


【解决方案1】:

为什么不能只遍历集合

r=set(["2","3"])
for item in r:
    sql="insert ignore into array (firt,last_name,arrays) values 'nina','sa',({})".format(item)
    cursor.execute(sql)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2021-06-05
  • 1970-01-01
  • 2011-04-10
  • 2014-06-12
  • 1970-01-01
相关资源
最近更新 更多