I have the following problem: I want to update an existing SQLite database row by row. What's happening now is that the iterator updates all existing rows with the last assigned value of dbdata
.
我有以下问题:我想逐行更新现有的SQLite数据库。现在发生的情况是,迭代器使用dbdata的最后赋值更新所有现有的行。
I don't want that. I want update row 1
with the first assigned value of dbdata
. Then take iterator shall go "up" again, get the next value and the updating should go on to the next row. Obviously there is a problem with the logic but I cannot get my head around it.
我不希望这样。我想用dbdata的第一个赋值来更新第一行。然后,迭代器将再次“向上”,得到下一个值,更新将继续到下一行。很明显,逻辑上有问题,但我想不出来。
Whats happening now is that the rows are updated now which leaves me with the last assigned value of dbdata
for all rows. I only want one row to be updated per iteration. How do I tell Python to always "go one row down"? Can someone give a hint? I am not looking for a complete solution here. My current code is as follows:
现在发生的情况是,现在更新了行,这给我留下了所有行的dbdatafor最后赋值。我只希望每次迭代更新一行。我如何告诉Python总是“向下一行”?有人能给个提示吗?我并不是在寻找一个完整的解决方案。我目前的代码如下:
for row in dbconnector:
print (row)
dbdata = langid.classify("{}".format(row))
print (dbdata)
connector.execute('''update SOMEDB set test1=? , test2=?''',(dbdata[-2], dbdata[-1]))
I am working with a SQLite Database and Python 3.3.
我正在使用一个SQLite数据库和Python 3.3。
2 个解决方案
#1
1
The reason all your data is set to the last dbdata is because your update isn't restricted to a single row so on each iteration all the rows are set to whatever dbdata you just processed. To restrict your update use a where clause so the only row affected is the one you want.
将所有数据设置为最后一个dbdata的原因是,您的更新并不局限于单个行,因此在每次迭代中,所有的行都被设置为您刚刚处理的任何dbdata。要限制更新,请使用where子句,以便惟一受影响的行是您想要的行。
#2
1
Solved it. Thanks for all the input!
解决它。谢谢大家的输入!
n = 0
for row in dbconnector:
print (row)
dbdata = langid.classify("{}".format(row))
print (dbdata)
for amount in row:
n += 1
print (n)
connector.execute('''update SOMEDB set test1=? , test2=? where rowid == ?''',(dbdata[-2], dbdata[-1], n))
That works! It alters with every iteration the number of the rowid.
那工作!它改变了rowid的每一次迭代次数。
#1
1
The reason all your data is set to the last dbdata is because your update isn't restricted to a single row so on each iteration all the rows are set to whatever dbdata you just processed. To restrict your update use a where clause so the only row affected is the one you want.
将所有数据设置为最后一个dbdata的原因是,您的更新并不局限于单个行,因此在每次迭代中,所有的行都被设置为您刚刚处理的任何dbdata。要限制更新,请使用where子句,以便惟一受影响的行是您想要的行。
#2
1
Solved it. Thanks for all the input!
解决它。谢谢大家的输入!
n = 0
for row in dbconnector:
print (row)
dbdata = langid.classify("{}".format(row))
print (dbdata)
for amount in row:
n += 1
print (n)
connector.execute('''update SOMEDB set test1=? , test2=? where rowid == ?''',(dbdata[-2], dbdata[-1], n))
That works! It alters with every iteration the number of the rowid.
那工作!它改变了rowid的每一次迭代次数。