SQLite itself does not natively support variables like other SQL dialects, but you can use in-memory temporary tables to emulate variable functionality. Here’s how to use it:
<code class="language-sql">BEGIN; /* 开始事务 */ PRAGMA temp_store = 2; /* 使用内存模式创建临时表 */ CREATE TEMP TABLE _Variables( Name TEXT PRIMARY KEY, RealValue REAL, IntegerValue INTEGER, BlobValue BLOB, TextValue TEXT );</code>
<code class="language-sql">INSERT INTO _Variables (Name) VALUES ('VariableName');</code>
<code class="language-sql">UPDATE _Variables SET IntegerValue = ... WHERE Name = 'VariableName';</code>
<code class="language-sql">... ( SELECT COALESCE(RealValue, IntegerValue, BlobValue, TextValue) FROM _Variables WHERE Name = 'VariableName' LIMIT 1 ) ...</code>
<code class="language-sql">DROP TABLE _Variables; END; /* 结束事务 */</code>
This method simulates the behavior of variables, allowing you to declare, assign and use variables in SQLite queries.
The above is the detailed content of How Can I Simulate Variables in SQLite?. For more information, please follow other related articles on the PHP Chinese website!