从列表中读入 MySql [英] Read from list into MySql

查看:50
本文介绍了从列表中读入 MySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my code to read from some data into MySql, the Mysql table contains 3 columns that in one of the columns I want to insert items of an array, each into a new row, and I do not want to have duplicate rows:

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] 作为好吧!

I have already looked into this post [1] as well !

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

However, on both of them I get this error:

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting

有什么建议吗?

推荐答案

为什么不能只迭代集合

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)

这篇关于从列表中读入 MySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆