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
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()
.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
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.
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: