开发者

Sqlite. How to get value of Auto Increment Primary Key after Insert, other than last_insert_rowid()?

开发者 https://www.devze.com 2023-01-11 06:40 出处:网络
I am using Sqlite3 with Flask microframework, but 开发者_运维问答this question concerns only the Sqlite side of things..

I am using Sqlite3 with Flask microframework, but 开发者_运维问答this question concerns only the Sqlite side of things..

Here is a snippet of the code:

g.db.execute('INSERT INTO downloads (name, owner, mimetype) VALUES (?, ?, ?)', [name, owner, mimetype])
file_entry = query_db('SELECT last_insert_rowid()')
g.db.commit()

The downloads table has another column with the following attributes: id integer primary key autoincrement,

If two people write at the same time the code above could produce errors.

Transactions can be messy. In Sqlite is there a neat built in way of returning the primary key generated after doing an INSERT ?


The way you're doing it is valid. There won't be a problem if the above snipped is executed concurrently by two scripts. last_insert_rowid() returns the rowid of the latest INSERT statement for the connection that calls it. You can also get the rowid by doing g.db.lastrowid.

0

精彩评论

暂无评论...
验证码 换一张
取 消