Save lua table to mysql file

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”} )

Where could posibly be the mistake, now??

You should use a table printing function and see what result is.   See:  http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/

When you print out the result table I’m going to bet it only has two members:  “data” and “name”.  You never retrieve tableoffactors from the row.

Rob

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:

[1,10]

[“4”,“30”]

[4,“10”]

May be a clue pointing to the mistake??
 

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:

SELECT data, name, tableoffactors FROM …

Rob

There it is!

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??