SQLite 更新选择(反之亦然)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将从先决条件开始:为什么不使用 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 theINSERT
fails.此讨论提出了两种可能的解决方案:
http://www.mail -archive.com/[电子邮件受保护]/msg10705.html
This discussion presents two possible solutions:
http://www.mail-archive.com/[email protected]/msg10705.html