Problem Inserting to sqlite the textinput from user and accessing it

I am trying to create a textbox and get user input into it. The user’s input will be stored inside a sqlite database table. But when I try to display the user’s input, I see no output on the screen.

location is the variable user enters and on pressing enter, is Inserted into the table. 

I feel like I’m missing some tiny logic. Please help !

Here is my piece of code:

NOTE: You can input in Textbox only in your OSX simulator, any device or use android simulator like Bluestacks. You can only see a grey box in Windows Corona SDK display.

require "sqlite3" local path = system.pathForFile("user.db", system.DocumentsDirectory) db = sqlite3.open( path ) local function onSystemEvent( event ) if( event.type == "applicationExit" ) then db:close() end end local tablesetup = [[CREATE TABLE IF NOT EXISTS place (id INTEGER PRIMARY KEY, location);]] print(tablesetup) db:exec( tablesetup ) local background = display.newRect(0,0,\_W,\_H) background:setFillColor(0,0,0) local locationTxtbox = native.newTextField(180,140,280,100) locationTxtbox.size = 34 locationTxtbox:addEventListener("textListener",locationTxtbox) local function textListener( event ) --user enters textbox if ( event.phase == "began" ) then -- user begins editing defaultField event.target.text = '' --user touches somewhere outside the textbox elseif ( event.phase == "ended" ) then -- do something with defaultField text local location = event.target.text --user types something elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) --user presses enter after typing, add to database table elseif ( event.phase == "submitted" ) then local location = event.target.text local tablefill =[[INSERT INTO place VALUES ("40",']]..location..[[');]] print( event.target.text ) db:exec(tablefill) end end --Displaying whatever user entered in the textbox and pressed enter. for row in db:nrows("SELECT \* FROM place") do local text = row.location local t = display.newText(text, 250, 120, native.systemFont, 40) end Runtime:addEventListener( "system", onSystemEvent )

Perhaps the data is not getting entered for some reason. Have you checked the database itself, using a tool like SQLite browser in Project Sandbox files?

Did you want to add a button press to retrieve the data, or did you want it automatically displayed? Using a button to get the data works well,

local function generate(event) for row in db:nrows("SELECT \* FROM place") do local text = row.location local t = display.newText(text, 250, 120, native.systemFont, 40) end end

You have a scope issue and how you set up your listener is also suspect.  In Corona there are two ways to do listeners:  table listeners and function listeners.

When you define your event listener like:

locationTxtbox :addEventListener(“textListener”, locationTxtbox )

where you pass the object in as the parameter, you’re doing a “table listener”.   To do this you must have previously defined your function either:

locationTxtbox:someFunctionName( event )

end

or:

local function someFunctionName( self, event )

end

then:

locationTxtbox.textListener = someFunctionName

Both the function and setup of the .textListener have to be defined before you do :addEventListener().

For a function listener (which I prefer), you simply declare the function first:

local function someFunctionName ( event )

end

Then when you’re ready to do the event listener:

locationTxtbox :addEventListener(“textListener”, someFunctionName )

regardless you define your function later, which means the values you pass to the event handler are “nil” which means do nothing.

Rob

Hi Rob !

 Thanks for the reply. I tried out some other method. And I declared the function listener before using it. But I still got issues, which I again had to post in the forum: 

http://forums.coronalabs.com/topic/55139-global-variables-have-nil-value-attempt-to-call-global-a-nil-value/

I found out that the database is getting created and also the values were inserted if I use a constant string like,

INSERT INTO table VALUES(“value1”,“value2”,“OtherValues”);

But, the database remained empty when I tried to extract from text input.

That seems to be the problem. The code doesn’t extract the text entered in the textbox, and so no values are inserted into the database. 

Perhaps the data is not getting entered for some reason. Have you checked the database itself, using a tool like SQLite browser in Project Sandbox files?

Did you want to add a button press to retrieve the data, or did you want it automatically displayed? Using a button to get the data works well,

local function generate(event) for row in db:nrows("SELECT \* FROM place") do local text = row.location local t = display.newText(text, 250, 120, native.systemFont, 40) end end

You have a scope issue and how you set up your listener is also suspect.  In Corona there are two ways to do listeners:  table listeners and function listeners.

When you define your event listener like:

locationTxtbox :addEventListener(“textListener”, locationTxtbox )

where you pass the object in as the parameter, you’re doing a “table listener”.   To do this you must have previously defined your function either:

locationTxtbox:someFunctionName( event )

end

or:

local function someFunctionName( self, event )

end

then:

locationTxtbox.textListener = someFunctionName

Both the function and setup of the .textListener have to be defined before you do :addEventListener().

For a function listener (which I prefer), you simply declare the function first:

local function someFunctionName ( event )

end

Then when you’re ready to do the event listener:

locationTxtbox :addEventListener(“textListener”, someFunctionName )

regardless you define your function later, which means the values you pass to the event handler are “nil” which means do nothing.

Rob

Hi Rob !

 Thanks for the reply. I tried out some other method. And I declared the function listener before using it. But I still got issues, which I again had to post in the forum: 

http://forums.coronalabs.com/topic/55139-global-variables-have-nil-value-attempt-to-call-global-a-nil-value/

I found out that the database is getting created and also the values were inserted if I use a constant string like,

INSERT INTO table VALUES(“value1”,“value2”,“OtherValues”);

But, the database remained empty when I tried to extract from text input.

That seems to be the problem. The code doesn’t extract the text entered in the textbox, and so no values are inserted into the database.