SQLite 更新选择(反之亦然)

发布于 2024-09-18 03:45:21 字数 591 浏览 10 评论 0原文

SQLite 中是否有一种单语句选择和更新(或更新和选择)方法?

触发器可以调用 select,但不允许在表达式中使用更新:(

CREATE TABLE id ( a integer );
CREATE TRIGGER idTrig AFTER UPDATE ON id BEGIN SELECT old.a FROM id; END;
INSERT INTO id VALUES ( 100 );
INSERT INTO test VALUES ( (UPDATE id SET a=a+1) ); -- syntax error

触发的 select 只能通过 C API 访问吗?)

我从单个 ID 数据库(具有单行)生成多个数据库的对象 ID以获得下一个可用的 ID)。我想在一个语句中选择并更新 ID 数据库,以便附加 ID 数据库的并发数据库连接不会遇到此问题(其中两个连接可以在任一更新之前插入):

INSERT INTO tab VALUES ( (SELECT uuid||oid AS oid FROM id.tab), ... );
UPDATE id.tab SET oid = oid+1;

Is there a one-statement select-and-update (or update-and-select) method in SQLite?

A trigger can invoke select, but that doesn't allow update to be used in an expression:

CREATE TABLE id ( a integer );
CREATE TRIGGER idTrig AFTER UPDATE ON id BEGIN SELECT old.a FROM id; END;
INSERT INTO id VALUES ( 100 );
INSERT INTO test VALUES ( (UPDATE id SET a=a+1) ); -- syntax error

(Is a triggered select only accessible via the C API?)

I generate object IDs for several databases from a single ID database (with a single row for the next available ID). I'd like to select-and-update on the ID db in one statement, so that concurrent db connections which attach the ID db won't have trouble with this (where two connections could insert before either updates):

INSERT INTO tab VALUES ( (SELECT uuid||oid AS oid FROM id.tab), ... );
UPDATE id.tab SET oid = oid+1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-09-25 03:45:21

我将从先决条件开始:为什么不使用 GUID?他们不需要中央权威,因此效率更高,更容易合作。

如果您确实需要中央 ID 存储,则可以使 ID 成为自动增量并使用 SELECT last_insert_rowid() 获取它。如果您必须生成自己的 ID,则将 ID 列设为主键,这样您就可以生成 ID,INSERT 它,并在 INSERT 失败时重试。

I'll start with the prerequisite nag: why not use GUIDs? They don't require central authority and are thus more efficient and easier to work with.

If you really need a central ID store, you can make the ID an autoincrement and fetch it with SELECT last_insert_rowid(). If you have to generate your own IDs, then make the ID column a primary key, so you can generate the ID, INSERT it, and retry if the INSERT fails.

晌融 2024-09-25 03:45:21

此讨论提出了两种可能的解决方案:

http://www.mail -archive.com/[电子邮件受保护]/msg10705.html

This discussion presents two possible solutions:

http://www.mail-archive.com/[email protected]/msg10705.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文