拥有多个列表并将它们的值插入到 MySql 中 [英] Having multiple lists and inserting their values into MySql
问题描述
这是我从一些数据读取到 MySql 的代码,MySql 表包含 4 列,在 2 列中我想插入一个数组的项目,每个项目都插入一个新行.我能够在 MySql 中插入一个列表,但只是很想知道如果我有多个列表会发生什么.代码如下:
Here is my code to read from some data into MySql, the MySql table contains 4 columns, that in 2 columns I want to insert items of an array, each into a new row. I was able to insert one list into MySql , but just was curious to know what happens if I have more than one list. Here is the code:
cursor = db.cursor()
r=["2","3"]
b=["3","4","5","2"]
for x in r:
for a in b:
insertsql=("insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','"+x+"','"+a+"')")
这里是输出:
1 ni sa 2 2
2 ni sa 3 2
谁能解释一下为什么会这样?为什么我看不到第二个列表 (b) 的所有值?在这些情况下最好的方法是什么?
Can someone please explain why this is happening? Why I do not see all the values for the second list (b)? What is the best approach in these circumstances?
推荐答案
我不确定它是否能解决您的问题.但是,对我来说,这可能与您迭代的方式有关.示例:
I am not sure whether it solves your problem. But, to me it might be related to the way you are iterating. Example:
r=["2","3"]
b=["3","4","5","2"]
for x in r:
for a in b:
pass
print(("insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','"+x+"','"+a+"')"))
输出(您当前的输出):
Outputs(your current output):
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','2','2')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','3','2')
如果我们用一些东西改变它,它就会起作用
If we change it with something this it works
r=["2","3"]
b=["3","4","5","2"]
for x in r:
for a in b:
print(("insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','"+x+"','"+a+"')"))
输出(如预期):
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','2','3')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','2','4')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','2','5')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','2','2')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','3','3')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','3','4')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','3','5')
insert ignore into new_table (name,last,arrayeha,arrayeha_se) values ('ni','sal','3','2')
忽略SQL注入
r=["2","3"]
b=["3","4","5","2"]
for x in r:
for a in b:
print(("insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)",("ni",'sal',x,a)))
输出:
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '2', '3'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '2', '4'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '2', '5'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '2', '2'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '3', '3'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '3', '4'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '3', '5'))
('insert ignore into new_table (name,last,arrayeha,arrayeha_se) values (?,?,?,?)', ('ni', 'sal', '3', '2'))
这篇关于拥有多个列表并将它们的值插入到 MySql 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!