如何迭代整数并插入到SQL中

huangapple go评论71阅读模式
英文:

How can i itenerate int´s and insert into sql

问题

我正在尝试使用for循环获取Id并在表中更新产品的状态。目前有这段代码:

for row in range(self.tableLinhaNova.rowCount()): 

    _item = self.tableLinhaNova.item(row, column) 
    if _item:            
        items1 = self.tableLinhaNova.item(row, column).text()
        items= int(items1)
    
    for item in items:
       cursor.execute("""UPDATE "Teste" SET "Estado" = 2 WHERE "IdTeste" = %s""",[item])
     
       
       con.commit()

Items 是一些Id,如下:

18,17,83,14

但它不起作用,我收到了这个错误:

TypeError: 'int' object is not iterable

我是Python新手,所以我可能做错了什么,有人可以帮忙吗?

英文:

Im trying to use a for loop the get the Id´s and update in the table the state of the product. Right now have this code:

for row in range(self.tableLinhaNova.rowCount()): 

    _item = self.tableLinhaNova.item(row, column) 
    if _item:            
        items1 = self.tableLinhaNova.item(row, column).text()
        items= int(items1)
    
    for item in items:
       cursor.execute("""UPDATE "Teste" SET "Estado"= 2 WHERE "IdTeste" = %s""",[item])
     
       
       con.commit()

Items is some Id´s like:

18,17,83,14

But it doesn´t work, i am getting this error:

TypeError: 'int' object is not iterable

I´m new in python, so i probably did something wrong can anyone help please!.

答案1

得分: 0

items= int(items1) 将要么将某个整数赋给 items(如果 items1 可以被Python解释为整数),要么引发 ValueError 异常。在你的情况下,显然是前一种情况。我会假设 items1 是一个表示单个数字的字符串。

也许你需要类似这样的东西:

...
items1 = self.tableLinhaNova.item(row, column).text()
items = [int(i) for i in items1.split(',')]
...
英文:

items= int(items1) will either assign some integer to items (if items1 can be interpreted as an integer by Python) or raise a ValueError. In your situation, it's obviously the former case. I'd assume that items1 is a string representing a single number.

Maybe you'd need something like:

...
items1 = self.tableLinhaNova.item(row, column).text()
items = [int(i) for i in items1.split(',')]
...

huangapple
  • 本文由 发表于 2023年6月5日 17:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定