SQLite的。插入后如何获取自动递增主键的值,而不是last_insert_rowid()? [英] Sqlite. How to get value of Auto Increment Primary Key after Insert, other than last_insert_rowid()?

查看:735
本文介绍了SQLite的。插入后如何获取自动递增主键的值,而不是last_insert_rowid()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Sqlite3和Flask的微框架,但是这个问题只涉及Sqlite方面的事情。



这是代码片段:



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


$ b $ downloads 表中有另一列,其中包含以下属性: id整数主键自动增量,

如果两个人同时写上面的代码可能会产生错误。

交易可以乱七八糟在Sqlite中有一个整洁的内置方式,返回执行INSERT后生成的主键?解析方案

这样做是有效的。如果上面的剪切被两个脚本同时执行,则不会有问题。 last_insert_rowid()返回调用它的连接的最新INSERT语句的rowid。您还可以通过执行 g.db.lastrowid 来获得rowid。


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.

这篇关于SQLite的。插入后如何获取自动递增主键的值,而不是last_insert_rowid()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆