SQLite에서 변수를 선언하고 사용
SQLite에서 변수를 선언하고 insert
작동에 사용하고 싶습니다 .
MS SQL에서와 같이 :
declare @name as varchar(10)
set name = 'name'
select * from table where name = @name
예를 들어에서 가져 와서 last_insert_row
사용해야합니다 insert
.
바인딩에 대해 뭔가를 찾았지만 완전히 이해하지 못했습니다.
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;
Herman의 솔루션은 작동하지만 Sqlite는 모든 필드에 모든 값 유형을 저장할 수 있기 때문에 단순화 할 수 있습니다.
다음은 값을 저장하기 위해 Value
선언 된 하나의 필드 를 사용하는 더 간단한 버전입니다 TEXT
.
CREATE TEMP TABLE IF NOT EXISTS Variables (Name TEXT PRIMARY KEY, Value TEXT);
INSERT OR REPLACE INTO Variables VALUES ('VarStr', 'Val1');
INSERT OR REPLACE INTO Variables VALUES ('VarInt', 123);
INSERT OR REPLACE INTO Variables VALUES ('VarBlob', x'12345678');
SELECT Value
FROM Variables
WHERE Name = 'VarStr'
UNION ALL
SELECT Value
FROM Variables
WHERE Name = 'VarInt'
UNION ALL
SELECT Value
FROM Variables
WHERE Name = 'VarBlob';
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 the last_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()
.
PRAGMA foreign_keys = ON; -- sqlite foreign key support is off by default
PRAGMA temp_store = 2; -- store temp table in memory, not on disk
CREATE TABLE Foo(
Thing1 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
);
CREATE TABLE Bar(
Thing2 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
FOREIGN KEY(Thing2) REFERENCES Foo(Thing1)
);
BEGIN TRANSACTION;
CREATE TEMP TABLE _Variables(Key TEXT, Value INTEGER);
INSERT INTO Foo(Thing1)
VALUES(2);
INSERT INTO _Variables(Key, Value)
VALUES('FooThing', last_insert_rowid());
INSERT INTO Bar(Thing2)
VALUES((SELECT Value FROM _Variables WHERE Key = 'FooThing'));
DROP TABLE _Variables;
END TRANSACTION;
For read-only variables (using the same value throughout the query) use a Common Table Expression (CTE).
WITH var AS (SELECT 'name' AS name, 10 AS more)
SELECT *, (table.cost + var.more) AS result
FROM table, var
WHERE table.name = var.name
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
참고URL : https://stackoverflow.com/questions/7739444/declare-variable-in-sqlite-and-use-it
'programing tip' 카테고리의 다른 글
IIS7에서 교차 출처 리소스 공유 활성화 (0) | 2020.09.22 |
---|---|
일괄 정규화 및 드롭 아웃 순서? (0) | 2020.09.22 |
<and> 내부 탈출 방법 (0) | 2020.09.22 |
마스터를 Github에 Git-push 할 수 없음- 'origin'이 git 저장소로 표시되지 않음 / 권한 거부 됨 (0) | 2020.09.22 |
문자열 값이 열거 형 목록에 있는지 확인하는 방법은 무엇입니까? (0) | 2020.09.21 |