在 SQLite 中声明变量并使用它
我想在 SQLite 中声明一个变量并在 insert
操作中使用它。
就像在 MS SQL 中一样:
declare @name as varchar(10)
set name = 'name'
select * from table where name = @name
例如,我需要获取 last_insert_row
并在 insert
中使用它。
我发现了一些关于绑定的东西,但我并没有真正完全理解它。
I want to declare a variable in SQLite and use it in insert
operation.
Like in MS SQL:
declare @name as varchar(10)
set name = 'name'
select * from table where name = @name
For example, I will need to get last_insert_row
and use it in insert
.
I have found something about binding but I didn't really fully understood it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
SQLite 不支持本机变量语法,但您可以使用内存临时表实现几乎相同的效果。
我在大型项目中使用了以下方法,效果非常好。
SQLite doesn't support native variable syntax, but you can achieve virtually the same using an in-memory temp table.
I've used the below approach for large projects and works like a charm.
对于只读变量(即设置一次并在查询中的任何位置使用的常量值),请使用公共表表达式 (CTE)。
SQLite WITH 子句
For a read-only variable (that is, a constant value set once and used anywhere in the query), use a Common Table Expression (CTE).
SQLite WITH clause
Herman 的解决方案有效,但可以简化,因为 Sqlite 允许在任何字段上存储任何值类型。
下面是一个更简单的版本,它使用一个声明为
TEXT
的Value
字段来存储任何值:Herman's solution works, but it can be simplified because Sqlite allows to store any value type on any field.
Here is a simpler version that uses one
Value
field declared asTEXT
to store any value:在您的示例中使用 denverCR 的答案:
作为初学者,我发现其他答案太难以理解,希望这有效
To use the one from denverCR in your example:
As a beginner I found other answers too difficult to understand, hope this works
赫尔曼的解决方案对我有用,但
...
让我有点困惑。我包括了我根据他的回答制作的演示。我的答案中的附加功能包括外键支持、自动递增键以及使用last_insert_rowid()
函数获取事务中最后一个自动生成的键。当我遇到需要三个外键的事务但我只能使用
last_insert_rowid()
获取最后一个外键时,我就需要此信息。Herman's solution worked for me, but the
...
had me mixed up for a bit. I'm including the demo I worked up based on his answer. The additional features in my answer include foreign key support, auto incrementing keys, and use of thelast_insert_rowid()
function to get the last auto generated key in a transaction.My need for this information came up when I hit a transaction that required three foreign keys but I could only get the last one with
last_insert_rowid()
.创建“VARIABLE”以在 SQLite SELECT(和其他一些)语句中使用
Creating "VARIABLE" for use in SQLite SELECT (and some other) statements
阅读完所有答案后,我更喜欢这样的内容:
After reading all the answers I prefer something like this:
我经常需要对数据库中的案例进行事后分析。这涉及到运行一系列针对该组值进行过滤的查询,每次使用一组不同的值。
我发现拥有一个“变量表”非常有用,每个“变量”需要一列,我每次都设置我需要的值,并且一系列查询不会每次都改变。
如果您确保您的“变量”具有唯一的名称(该名称不会出现在您正在查询的表中),您可以进一步简化查询:
当我完成一组值时,我将返回到
DELETE 并为我的“变量”设置一组新值并继续。
最后,通过
警告
进行清理。请记住,所有这些都是在
PRAGMA temp_store = 2;
生效时进行的,因此任何数据创建都将尝试在内存中进行。还要注意数据库/连接的更改。在内存中创建的表与活动数据库或连接耦合。如果您更改数据库(认为“USE”)或在数据库编辑器/GUI 中更改连接,内存中的时态表将不会遵循:您将必须为下一个数据库或连接再次创建它们。这意味着,如果您要更改数据库以比较来自多个数据库的查询结果,请记住您的查询正在使用内存中当前关联的表中的值与您正在使用的数据库。因此,如果您将它们更改为在一个数据库中运行查询,然后更改为在另一个数据库中运行相同的查询,则需要在运行查询之前更改内存中的值,否则将不会运行相同的查询。
I frequently have to post-mortem analyse cases in a database. This involves running a series of queries filtering for the set of values, several times with a different set of values each time.
I found very useful to have a "variables table" with one column per "variable" needed, where I set the values I need each time, and having the series of queries not change every time.
If you ensure your "variables" have unique names (that do not occur in the tables you are querying), you may simplify even more the queries:
When I finish with one set of values I go back to the
DELETE
and set a new set of values for my "variables" and keep going.At the end, clean up by means of
WARNING
Keep in mind that all of this is made while
PRAGMA temp_store = 2;
is in force, so any data creation will try to go on memory.Also beware of database/connection changes. The tables created in memory are coupled with the active database or connection. If you change database (think "USE ") or in you database editor/GUI you change connection, the temporal tables in memory will not follow: you will have to create them again for the next database or connection. And that implies that if you are changing database to compare query results from more than one database, keep in mind that your queries are using the values from the tables in memory currently associated with the database you are using. It follows that if you change them to run a query in one database and then you change to run the same query in another database, you need change the values in memory before running the query or you will not be running the same query.
尝试使用绑定值。您不能像在 T-SQL 中那样使用变量,但可以使用“参数”。我希望以下链接有用。绑定值
Try using Binding Values. You cannot use variables as you do in T-SQL but you can use "parameters". I hope the following link is usefull.Binding Values
我找到了一种将变量分配给 COLUMN 或 TABLE 的解决方案:
不要忘记在 WHERE 和双引号之间输入一个空格
以及双引号和 LIKE 之间
I found one solution for assign variables to COLUMN or TABLE:
Don't forget input one space between the WHERE and double quotes
and between the double quotes and LIKE