This is driving me insane.
I am running DBeaver Version 23.3.4.202402060628 and I have the following SQLite3 code (for the sake of argument let’s pretend it is for a ‘highspeed injection moulded part and package tracking machine’ in a factory):
--------------------------------------------------------------------------------
-- SQLite TABLE
DROP TABLE IF EXISTS pkgdata;
CREATE TABLE pkgdata (
DateTime TEXT, -- source format = "DD.MM.YYYY HH:mm:ss.sss"
Weight REAL,
Height REAL,
Boxes INT,
Parts INT,
Spread REAL GENERATED ALWAYS AS (Weight - Height) STORED,
gen_pkg_year INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 7, 4)) STORED,
gen_pkg_month INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 4, 2)) STORED,
gen_pkg_day INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 1, 2)) STORED,
gen_pkg_wkday INT GENERATED ALWAYS AS (strftime('%w',DateTime)) STORED,
gen_pkg_per TEXT GENERATED ALWAYS AS (strftime('%p',DateTime)) STORED,
gen_pkg_hr INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 12, 2)) STORED,
gen_pkg_min INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 15, 2)) STORED,
gen_pkg_sec INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 18, 2)) STORED,
gen_pkg_ms INT GENERATED ALWAYS AS (SUBSTRING(DateTime, 21, 3)) STORED,
gen_ISODate TEXT GENERATED ALWAYS AS (
printf('%04d-%02d-%02d %02d:%02d:%02d.%03d',
gen_pkg_year, gen_pkg_month, gen_pkg_day,
gen_pkg_hr, gen_pkg_min, gen_pkg_sec, gen_pkg_ms)
) STORED,
prev_Weight REAL,
prev_Height REAL,
Weight_diff REAL,
Height_diff REAL,
Weight_move TEXT,
Height_move TEXT,
gap TEXT
);
--------------------------------------------------------------------------------
-- SQLite INDICES - created one line at a time
CREATE INDEX pkg_idx_ymd ON pkgdata (gen_pkg_year, gen_pkg_month, gen_pkg_day);
CREATE INDEX pkg_idx_mon ON pkgdata (gen_pkg_month);
CREATE INDEX pkg_idx_dom ON pkgdata (gen_pkg_day);
CREATE INDEX pkg_idx_dow ON pkgdata (gen_pkg_wkday);
CREATE INDEX pkg_idx_per ON pkgdata (gen_pkg_per);
CREATE INDEX pkg_idx_hr ON pkgdata (gen_pkg_hr);
CREATE INDEX pkg_idx_min ON pkgdata (gen_pkg_min);
--------------------------------------------------------------------------------
-- SQLite TRIGGER
DROP TRIGGER IF EXISTS pkg_tr_fill_calculated_columns;
CREATE TRIGGER pkg_tr_fill_calculated_columns
AFTER INSERT ON pkgdata
BEGIN
UPDATE pkgdata
SET prev_Weight = previous.Weight,
prev_Height = previous.Height,
Weight_diff = NEW.Weight - previous.Weight,
Height_diff = NEW.Height - previous.Height,
Weight_move = CASE
WHEN Weight > prev_Weight THEN 'H'
WHEN Weight = prev_Weight THEN 'S'
WHEN Weight < prev_Weight THEN 'L'
END,
Height_move = CASE
WHEN Height > prev_Height THEN 'H'
WHEN Height = prev_Height THEN 'S'
WHEN Height < prev_Height THEN 'L'
END,
gap = CASE
WHEN Height > prev_Weight THEN 'U'
WHEN Weight < prev_Height THEN 'D'
ELSE ''
END
FROM (SELECT * FROM pkgdata
ORDER BY gen_ISODate DESC
LIMIT 1, 1) AS previous -- get the previous row
WHERE pkgdata.DateTime = NEW.DateTime;
END;
--------------------------------------------------------------------------------
-- SQLite INSERT DATA
INSERT INTO pkgdata (
DateTime, -- TEXT
Weight, -- REAL
Height, -- REAL
Boxes, -- INT
Parts) -- INT
VALUES
('01.01.2010 00:00:00.817',0.89755,0.89725,0,0),
('01.01.2010 00:00:01.157',0.89753,0.89728,0,0),
('01.01.2010 00:00:01.457',0.89755,0.89725,0,0),
('01.01.2010 00:00:01.737',0.89755,0.89725,0,0),
('01.01.2010 00:00:02.417',0.89755,0.89725,0,0),
('01.01.2010 00:00:02.747',0.8975,0.8973,0,0),
('01.01.2010 00:00:06.295',0.89755,0.89725,0,0),
('01.01.2010 00:00:06.566',0.89758,0.89723,0,0),
('01.01.2010 00:00:06.866',0.89755,0.89725,0,0),
('01.01.2010 00:00:09.985',0.89755,0.89725,0,0),
('01.01.2010 00:00:10.325',0.89755,0.89725,0,0),
('01.01.2010 00:00:11.055',0.89755,0.89725,0,0),
('01.01.2010 00:00:12.385',0.89755,0.89725,0,0),
('01.01.2010 00:00:12.604',0.8975,0.8973,0,0),
('01.01.2010 00:00:12.884',0.89755,0.89725,0,0),
('01.01.2010 00:00:13.114',0.89758,0.89723,0,0),
('01.01.2010 00:00:15.283',0.89758,0.89723,0,0),
('01.01.2010 00:00:15.514',0.8975,0.8973,0,0),
('01.01.2010 00:00:15.684',0.8975,0.8973,0,0),
('01.01.2010 00:00:17.083',0.8975,0.8973,0,0),
('01.01.2010 00:00:17.693',0.8975,0.8973,0,0)
returning *;
For the life of me I cannot get this to work.
I had previously working syntax from this question here (thanks to @zegarek), but now I am constantly getting the following error for this code when I get TO the last step – the INSERT
statement.
DBeaver accepts each block fine (except I have to create each index line by line), but when I get to the INSERT statement it always fails with the following error:
SQL Error [1]: [SQLITE_ERROR] SQL error or missing database (ambiguous column name: Weight)
I just do not understand why.
This makes absolutely no sense to me – there is only one table with one column named ‘Weight’.
I read this post, and thought that maybe there is something still open in memory, so I disconnected from all other databases and deleted all tables from this one and started from scratch, but it still throws this error!
I even tried using fully qualified column names by prepending the table name like so:
INSERT INTO pkgdata (
pkgdata.DateTime, -- TEXT
pkgdata.Weight, -- REAL
pkgdata.Height, -- REAL
pkgdata.Boxes, -- INT
pkgdata.Parts) -- INT
VALUES
...
thinking this would remove the ambiguity, but then it throws a syntax error on the period .
.
Any suggestions?