johnslattery
9/1/2016 - 7:41 PM

A fairly commonplace solution to the lack of variables in SQLite.

A fairly commonplace solution to the lack of variables in SQLite.

DROP TABLE IF EXISTS variables;
CREATE TEMP TABLE variables (
  name PRIMARY KEY NOT NULL,
  val_real REAL,
  val_integer INTEGER,
  val_blob BLOB,
  val_text TEXT);
  
-- Declare.
INSERT INTO variables (name) VALUES ('some_name');

-- Set.
UPDATE variables SET val_text = 'some_value' WHERE name = 'some_name' LIMIT 1;

-- Reference.
(SELECT coalesce(v.val_real, v.val_integer, v.val_blob, v.val_text)
  FROM variables v WHERE v.name = 'some_name')