python - 从列表中读入MySql

标签 python mysql

这是我从一些数据读入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

请问有什么建议吗?

最佳答案

为什么你不能迭代集合

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)

关于python - 从列表中读入MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51791406/

相关文章:

python - 你能在 Python 列表理解中嵌入一个 try 吗?

python - 如何动态迭代上游任务的输出以在 Airflow 中创建并行任务?

mysql - 使用 Servlet、JSP、EJB 更新成员(member)(在网站上注册)的一个(或多个)字段

mysql - Jade不会显示mysql的结果

python - 在没有syncdb的情况下使用Django时出现"Relation not found"错误

python - 将字典转换为数据框,键作为列名,键的值作为数据框的列值

python - 为现有值中的新值列创建循环

mysql - 我可以为多个 mysql 命令行调用输入一次密码吗?

mysql - 使用 moment.js 处理希伯来语日期

php - 有什么方法可以在 PHP 中保持与 mysql 数据库的连接?