插入SQLite数据库的行数为负 [英] Number of rows inserted into SQLite db is negative
本文介绍了插入SQLite数据库的行数为负的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Python
代码向SQLite
数据库中插入新记录.
I'm trying to insert new records into SQLite
database from Python
code.
con = sqlite.connect(connectionString)
cur = con.cursor()
countOfNewItems = 0
for ...
try:
con.execute("insert or ignore into items ...")
countOfNewItems += cur.rowcount
except:
cur.close()
con.close()
print "Error when inserting item '%s' to database." % item
exit(1)
cur.close()
con.commit()
con.close()
print "%d new items have been inserted." % countOfNewItems
我的代码报告插入的记录数为负数(-5141).
My code reports negative number of inserted records (-5141).
因为我的数据库为空,所以我可以找出通过命令行插入了多少条记录
Because my database was empty, I could find out how many records were inserted via command line
select count(*) from items;
4866
您能告诉我怎么了吗?为什么两个值不匹配,为什么它为负?
Could you advise me what's wrong. Why the two values don't match and why it's negative?
推荐答案
尝试使用cur.execute
而不是con.execute
. cur.rowcount
然后为我返回1以进行简单插入.
Try cur.execute
instead of con.execute
. cur.rowcount
then returns 1 for me for a simple insert.
这篇关于插入SQLite数据库的行数为负的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文