This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Table and CSV
#1
Hello

I created a scrip that populates a 'MyPowerLog' table for me
below

Code:
data = storage.get('MyPowerLog', {})

  table.insert(data, {
    -- usare os.date formattare la stringa
    ['time'] = os.time(),
    ['Totale Attiva'] = grp.getvalue('4/0/108'),
    ['Totale Reattiva'] = grp.getvalue('4/1/108'),
  })
storage.set('MyPowerLog', data)

I should export it to CSV only the data of the last month as I can do

Furthermore, once the data has been exported, I should delete it in order not to have a continuous table that increases the space occupied dramatically. the script command to delete the table which is it?


Thanks so much
Reply
#2
Use this to delete the storage entry:
Code:
storage.delete('MyPowerLog')
Reply
#3
(08.06.2021, 06:49)admin Wrote: Use this to delete the storage entry:
Code:
storage.delete('MyPowerLog')
Where is the mistake?

i read data from my table, prepare csv header, look for data in my table and for each row i would like to add them to csv file, but nothing i have only headers.

Code:
--date.day, date.hour, date.min, date.sec = 1, 0, 0, 0
--ts_start = os.time(date)
--date.month = date.month + 1
--ts_end = os.time(date)
--PowTable = db:getall('SELECT * FROM MyPowerLog where time BETWEEN ? AND ? ORDER BY id DESC', ts_start, ts_end)

PowTable = db:getall('SELECT * FROM MyPowerLog')

-- csv buffer
buffer = {}
-- format csv row
csv = string.format('%q,%q,%q', "Time", "Attiva", "Reattiva")
-- add to buffer
table.insert(buffer, csv)
-- add empty line to buffer
table.insert(buffer, "")

-- Loop through PowTable
for _, Measure in ipairs(PowTable) do
  -- format csv row
  csv = string.format('%q,%q,%q', os.date("%d.%m.%Y %X", Measure.time),Measure.Attiva, Measure.Reattiva )
  -- add to buffer
  table.insert(buffer, csv)
end

--Create attachment inside FTP server
src = os.date('%Y-%m') .. '.csv'
dst = '/home/ftp/' .. src
io.writefile(dst, buffer)
i probably wrong something in the table reading loop but i can't figure out what.

thank you
Reply
#4
Your log in not a database table but a storage entry, so use this:
Code:
PowTable = storage.get('MyPowerLog', {})
Reply
#5
(08.06.2021, 08:40)admin Wrote: Your log in not a database table but a storage entry, so use this:
Code:
PowTable = storage.get('MyPowerLog', {})

Found problem my error now work all

Thank
Reply


Forum Jump: