lundi 13 juillet 2015

sqlite: can a db-name, table-name, column-name be an expression?

I want to fill a sql-table by update-function and choosing the columns in an expression like this:

update marks set (select name from students where id='1')='AAA'
where subject='math';

To explain it simply. I'd like to use an expression for column-name in a trigger to create a relational table value by value using the SET operator.

table students:
id name
 1 Max
 2 Joe
 3 Jim

table marks:
subject Max  Joe  Jim
math    AAA  B    C
sport   NULL NULL A

In my case a phyton script fills the table 'temps' row by row with sensor readings, 'SensorID' and 'Timestamp'. The lookup table 'sensors' can tell the 'SensorName' according each 'SensorID'. Now a trigger shall automatically fill a pivot table 'T1Table' with a timestamp of zeroed seconds substr(new.timestamp,1,17)||'00'.

timestamp           T1   T2   T3
2015-01-01 12:35:00 23.5 22.3 NULL

The source code would look like this:

CREATE TRIGGER insert_T1 AFTER INSERT ON temps
BEGIN
INSERT OR IGNORE INTO T1Table(TimeStamp) VALUES(substr(new.timestamp,1,17)||'00');
UPDATE T1Table SET (select sensorName from sensors where sensors.sensorID=new.sensorID)=
ROUND(new.temperature,1)
WHERE TimeStamp=substr(new.timestamp,1,17)||'00';
END;

unfortunately it doesn't work as I like, this is the error message

Error: near "(": syntax error

it seems the SET operator expects a hard coded entry. http://ift.tt/1NIZoHZ

Aucun commentaire:

Enregistrer un commentaire