19.05.2020, 16:08
Hello,
I did some tests with the next code. I want to insert all the trending values in my SQL database. When I run the script all the code executes correctly without errors in the logs but in the database I don't see the new values..
I'm not a programmer and It's my first time doing a script with LUA so I apologize in advance for the posible errors.
I did some tests with the next code. I want to insert all the trending values in my SQL database. When I run the script all the code executes correctly without errors in the logs but in the database I don't see the new values..
Code:
require('trends')
require("luasql.mysql");
--Get a list of all trendings
names = db:getlist('SELECT name FROM trends')
log (names)
--Defines a period of the last day
dates = {}
dates['start'] = os.date('*t')
dates['start'].day = dates['start'].day - 1
dates['end'] = os.date('*t')
env = luasql.mysql();
--DATABASE CONNECTION -> (DB_NAME, USERNAME, PASSWORD, MYSQL_SERVER_IP, MYSQL_SERVER_PORT)
dbcon, err = env:connect('CONFIDENTIAL', 'admin','CONFIDENTIAL', 'lm-database.CONFIDENTIAL.us-east-1.rds.amazonaws.com', '3306');
log(dbcon, err)
if dbcon then
-- Iterates all the trendings
for _, name in ipairs(names) do
-- Get all the trending values
data = trends.fetch(name, dates)
-- Iterates all the trending values
for id, val in pairs(data) do
-- Insert the trending values in the database.
dbcon:execute('INSERT INTO trending-definition (id, value) VALUES (id,val)')
end
end
else
log('mysql connection failed ' .. tostring(err))
end
dbcon:close()
env:close()
I'm not a programmer and It's my first time doing a script with LUA so I apologize in advance for the posible errors.