gpt4 book ai didi

mysql - 拥有多个列表并将它们的值插入 MySql

转载 作者:行者123 更新时间:2023-11-29 17:12:10 27 4
gpt4 key购买 nike

这是我从一些数据读取到 MySql 的代码,MySql 表包含 4 列,我想在 2 列中插入数组的项目,每个项目插入一个新行。我能够将一个列表插入 MySql ,但只是想知道如果我有多个列表会发生什么。这是代码:

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) 的所有值?在这种情况下最好的方法是什么?

最佳答案

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

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

输出(您当前的输出):

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

如果我们用一些东西改变它,它就会起作用

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注入(inject)

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 - 拥有多个列表并将它们的值插入 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51824426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com