我正在使用sqlite,我有一个Python
代码,如下所示:
...
cur.execute("insert or ignore into books (title, authors, ...) \
values (:title, :authors, ..."), locals())
...
bookId = cur.lastrowid
如果适用
则
ignore
的值为select statement
。但这不是我想要的。我想从数据库中获取
cur.lastrowid
值无论如何。
我应该使用
0
语句还是有更聪明的方法来实现它?我的临时解决方案:
if bookId == 0:
cur.execute("select id from books where \
title = :title and authors = :authors", locals())
bookId = cur.fetchone()[0]
最佳答案
没有更好的方法。
要从数据库中读取一些现有值,只有SELECT
。
关于python - “插入或忽略”后lastrowid的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17369233/