gpt4 book ai didi

python - 插入 SQLite 数据库的行数为负数

转载 作者:行者123 更新时间:2023-12-02 15:40:04 24 4
gpt4 key购买 nike

我正在尝试通过 Python 代码将新记录插入到 SQLite 数据库中。

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

因为我的数据库是空的,所以我可以通过命令行找出插入了多少条记录

从项目中选择 count(*);

4866

你能告诉我哪里出了问题吗?为什么这两个值不匹配,为什么是负数?

最佳答案

http://docs.python.org/library/sqlite3.html#sqlite3.Cursor.rowcount

Although the Cursor class of the sqlite3 module implements this attribute, the database engine’s own support for the determination of “rows affected”/”rows selected” is quirky.

As required by the Python DB API Spec, the rowcount attribute “is -1 in case no executeXX() has been performed on the cursor or the rowcount of the last operation is not determinable by the interface”.

关于python - 插入 SQLite 数据库的行数为负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9056679/

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