how to save data from a nativeTextField to sqlite database in corona

Having a hard time saving data from a native text field to sqlite database in corona.

here are some relevant code:

[code]function printrecord()
for row in db:nrows(“SELECT * FROM test”) do
t = display.newText(row.pname … " " … row.age … " " … row.desc, 20, 30 * row.id, null, 16)
t:setTextColor(255,255,255)
end
end

newData = native.newTextField ( 20, _H - 90, 280, 30 )
newData.inputType = “text”

saveData = function ( event )
textString = newData.text
db:exec( [[INSERT INTO test VALUES (NULL, textString, 30, “unknown”)]] )
t:removeSelf()
t = nil
printrecord()
end

savebutton = widget.newButton {
default = “buttonGreen.png”,
over = “buttonGreenOver.png”,
label = “Save”,
embose = true,
onRelease = saveData
} [/code]

when I try to change the textString from db:exec( [[INSERT INTO test VALUES (NULL, textString, 30, "unknown")]] ) to a string like “this is a string” it seems to work fine, can anyone help? [import]uid: 189861 topic_id: 33226 reply_id: 333226[/import]

This one is pretty simple… In Lua, [[and]] are treated like quote marks. That whole line:

[[INSERT INTO test VALUES (NULL, textString, 30, “unknown”)]]

is a string. There is no variable substitution going on. SQLite doesn’t know what textString is. It’s not a number, it’s not a string since it’s not in quotes and it’s not a reserved word. It doesn’t know about your Lua variables. The fix is uber simple.

[[INSERT INTO test VALUES (NULL, “]] …textString … [[”, 30, “unknown”)]]

Some important things to note about this. We end the first part of the string by closing the ]]'s. Then we use Lua’s string concatenate operator, the two …'s to fit your lua varable into the query string. Then we concatenate the rest of your query string back in, starting that part of the string with the [['s to match the ending]]'s.

Now note the double quotes. These are very important because with out them SQLite will have no idea that your data in textString is actually a string, it could be more SQL or a number. Let’s say that your textString contains the value “Barney Rubble”, the resulting query string would be:

INSERT INTO test VALUES (NULL, “Barney Rubble”, 30, “unknown”)

which is valid SQL. [import]uid: 19626 topic_id: 33226 reply_id: 132085[/import]

For future debugging I recommend putting your queries in a string, then execute with db:exec( myQuery ).

When things do not seem to work, output the query in the terminal, print(myQuery), then open the db in Firefox SQLite manager (https://addons.mozilla.org/en-us/firefox/addon/sqlite-manager/). Execute it there and you should get a better idea of what is going on.

It also helps to check for error so you know if what is supposed to happen actually happened.

 local query = [[INSERT INTO test VALUES (NULL, "]] ..textString .. [[", 30, "unknown")]]  
 local result = db:exec( query )  
 if result == 1 then   
 print('Something went wrong with insert query', query)   
 end  

Regards,
Jonjon [import]uid: 65415 topic_id: 33226 reply_id: 132090[/import]

Well that solve everything, thank you sir… [import]uid: 189861 topic_id: 33226 reply_id: 132130[/import]

This one is pretty simple… In Lua, [[and]] are treated like quote marks. That whole line:

[[INSERT INTO test VALUES (NULL, textString, 30, “unknown”)]]

is a string. There is no variable substitution going on. SQLite doesn’t know what textString is. It’s not a number, it’s not a string since it’s not in quotes and it’s not a reserved word. It doesn’t know about your Lua variables. The fix is uber simple.

[[INSERT INTO test VALUES (NULL, “]] …textString … [[”, 30, “unknown”)]]

Some important things to note about this. We end the first part of the string by closing the ]]'s. Then we use Lua’s string concatenate operator, the two …'s to fit your lua varable into the query string. Then we concatenate the rest of your query string back in, starting that part of the string with the [['s to match the ending]]'s.

Now note the double quotes. These are very important because with out them SQLite will have no idea that your data in textString is actually a string, it could be more SQL or a number. Let’s say that your textString contains the value “Barney Rubble”, the resulting query string would be:

INSERT INTO test VALUES (NULL, “Barney Rubble”, 30, “unknown”)

which is valid SQL. [import]uid: 19626 topic_id: 33226 reply_id: 132085[/import]

For future debugging I recommend putting your queries in a string, then execute with db:exec( myQuery ).

When things do not seem to work, output the query in the terminal, print(myQuery), then open the db in Firefox SQLite manager (https://addons.mozilla.org/en-us/firefox/addon/sqlite-manager/). Execute it there and you should get a better idea of what is going on.

It also helps to check for error so you know if what is supposed to happen actually happened.

 local query = [[INSERT INTO test VALUES (NULL, "]] ..textString .. [[", 30, "unknown")]]  
 local result = db:exec( query )  
 if result == 1 then   
 print('Something went wrong with insert query', query)   
 end  

Regards,
Jonjon [import]uid: 65415 topic_id: 33226 reply_id: 132090[/import]

Well that solve everything, thank you sir… [import]uid: 189861 topic_id: 33226 reply_id: 132130[/import]

can i have the whole code of this Sir wwwdotphilip? because, i’m using Windows and i’m having a hard time debugging because i need to build it before i’ll know if its working.

can i have the whole code of this Sir wwwdotphilip? because, i’m using Windows and i’m having a hard time debugging because i need to build it before i’ll know if its working.