插入SQLite数据库的行数为负 [英] Number of rows inserted into SQLite db is negative

查看:119
本文介绍了插入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆