"插入或忽略"后lastrowid的值

xra*_*alf 6 python sql sqlite insert

我有一个Python代码如下:

...
cur.execute("insert or ignore into books (title, authors, ...) \
values (:title, :authors, ..."), locals())
...
bookId = cur.lastrowid
Run Code Online (Sandbox Code Playgroud)

如果ignore部分select statement适用

然后的值cur.lastrowid0.

但这不是我想要的.books.id无论如何,我想从数据库中获取价值.

我应该使用select声明还是有更聪明的方法来实现它?

我的临时解决方案

if bookId == 0:
    cur.execute("select id from books where \
    title = :title and authors = :authors", locals())
    bookId = cur.fetchone()[0]  
Run Code Online (Sandbox Code Playgroud)

CL.*_*CL. 6

没有更好的方法.要从数据库中读取一些现有值,只有SELECT.

  • 我很惊讶没有更好的方法。坦率地说,似乎非常愚蠢,不得不做一个额外的选择。 (3认同)
  • 我相信你是因为你的声誉:-)谢谢 (2认同)