且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拥有多个列表并将它们的值插入到 MySql 中

更新时间:2022-12-12 15:47:07

我不确定它是否能解决您的问题.但是,对我来说,这可能与您迭代的方式有关.示例:

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'))