I am using a sql file to save my app’s persistent data, I have a lua table in my program now containing indicized data, my question is: is it possible to save the table straight to the mysql file and retrieve it with its containt as any other data saved to the mysql file?
That’s the point I get no error, but in the sql file there’s no new row new added. It’s like the function just fails to perform. And everything was working ok before the addition of the new value to the row when saved…
Got it. It was simply I was using and older mysql file which wasn’t initialized with the new value yet. Basically trashed the old file and now it’s working with new one…
But now I’m not being able to retrieve the table. When I try through this command
local sql = [[SELECT data, name from info WHERE datetime = "]] .. tostring(row.t3.text) .. [["]] for row in db:nrows(sql) do result = row end print (result.tableoffactors)
I get printed ‘nil’, with tableoffactors being the table we previously saved formatted with json and then supposed to be a string ( it shows like this the sql file : {“y”:“40”,“x”:“2”} )
I am not trying to retrieve the whole sql row in this case. Here I’m retrieving the sql row value ‘tableoffactors’, which supposedly is a lua table encoded into a json string. So stored to sql as a string basically.
But when I try to load it and decode it through json.decode (result.tableoffactors) I get the error : json.decode called with nil string
and in the sql file the string in question presents itself like this :
{“y”:10,“x”:1}
So where’s the error?
EDIT: -----
also trying to intialize the table before encoding it to json without a key-pair returns the same error. It present a strange behavior though: sometimes it saves the values contained in the table with double quotes and sometimes it doesn’t:
When you do: SELECT field1, field2 FROM … It only grabs the fields named “field1” and “field2” from the table. You only asked it to get “data” and “name”. Therefore your “result” table will have:
result.data
result.name
and no other fields. If you expect that table to have a member named “tableoffactors”, you need to change your select statement to:
Sorry, I didn’t understand you meant this when you said
You never retrieve tableoffactors from the row.
you were right, now it works…
Thanks a lot Rob!
A general question if you wish to answer:
In cases like this where you need to save several tables containing similar data, but each table having a different purpose, is it good practice to inglobate all those tables into a single one and save only this big table to the sql file and then load it every time and retrieve from it the specific sub-table you need? Or is it better keep each table as a single compartment and save each of them to a different VALUE in the sql file??
That’s the point I get no error, but in the sql file there’s no new row new added. It’s like the function just fails to perform. And everything was working ok before the addition of the new value to the row when saved…