19.11.2019, 19:12
(19.11.2019, 14:07)admin Wrote: Example script. Map table keys are trend ID from external database, values are group addresses to get values from. Don't forget to change access details in connect function.
Code:require('luasql.mysql') env = luasql.mysql() dbcon, err = env:connect(DB_NAME, USERNAME, PASSWORD, MYSQL_SERVER_IP, MYSQL_SERVER_PORT) if dbcon then map = { [1] = '1/1/1', [2] = '1/1/2', } for id, addr in pairs(map) do value = grp.getvalue(addr) dbcon:execute('INSERT INTO trends_values (trend_id, value, created_at) VALUES (' .. id .. ', ' .. value .. ', NOW())') dbcon:execute('UPDATE trends_definition SET updated_at=NOW() WHERE id=' .. id) end dbcon:close() else log('mysql connection failed ' .. tostring(err)) end env:close()
This code creates the tables (trends_values, trends_definition) automatically or I must create it manually in the database?