我想在sqlite中声明一个变量,并在插入操作中使用它
像在MS sql
- Declare @name as varchar(10)
- set name = 'name'
- Select * from table where name = @name
例如,我将需要获取last_insert_row并在插入中使用它
我发现了一些关于绑定,但我并没有真正完全理解它
sqlite不支持本地变量语法,但是您可以使用内存临时表实现相同的功能。
我对大型项目使用下面的方法,并像一个魅力。
- /* Create in-memory temp table for variables */
- BEGIN;
- PRAGMA temp_store = 2;
- CREATE TEMP TABLE _Variables(Name TEXT PRIMARY KEY,RealValue REAL,IntegerValue INTEGER,BlobValue BLOB,TextValue TEXT);
- /* Declaring a variable */
- INSERT INTO _Variables (Name) VALUES ('VariableName');
- /* Assigning a variable (pick the right storage class) */
- UPDATE _Variables SET IntegerValue = ... WHERE Name = 'VariableName';
- /* Getting variable value (use within expression) */
- ... (SELECT coalesce(RealValue,IntegerValue,BlobValue,TextValue) FROM _Variables WHERE Name = 'VariableName' LIMIT 1) ...
- DROP TABLE _Variables;
- END;