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.lastrowid是0.
但这不是我想要的.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)
没有更好的方法.要从数据库中读取一些现有值,只有SELECT.