1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- CREATE TABLE IF NOT EXISTS var_type (
- slabel CHAR(1) UNIQUE PRIMARY KEY,
- label VARCHAR(10) UNIQUE,
- type VARCHAR(10) NOT NULL,
- start_address INT NOT NULL DEFAULT 0,
- end_address INT NOT NULL DEFAULT 250
- );
-
- CREATE TABLE IF NOT EXISTS variable (
- type CHAR(1) NOT NULL,
- address INT NOT NULL,
- unit VARCHAR(5) NULL,
- last_update INT NULL,
-
-
- FOREIGN KEY (type) REFERENCES var_type(slabel)
- ON DELETE CASCADE,
- PRIMARY KEY(type, address)
- );
-
- CREATE TABLE IF NOT EXISTS var_value (
- type CHAR(1) NOT NULL,
- address INT NOT NULL,
- time INT DEFAULT (strftime('%s', datetime('now'))),
- value INT NOT NULL,
-
- FOREIGN KEY (type) REFERENCES variable(type)
- ON DELETE CASCADE,
- FOREIGN KEY (address) REFERENCES variable(address)
- ON DELETE CASCADE,
- PRIMARY KEY(type, address, time)
- );
-
- INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
- 'A', 'Analog', 'float', 1, 1250);
- INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
- 'I', 'Integer', 'int', 5002, 6252;
- INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
- 'D', 'Digital', 'bool', 1, 1000);
-
- CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
- FOR EACH ROW
- BEGIN
- UPDATE variable
- SET last_update = NEW.time
- WHERE
- type = NEW.type
- AND
- address = NEW.address;
- END;
|