我正在使用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/

10-14 19:31