How the User input from text field gets updated in Sqlite database.

Hi,

I am working on a app and i am new to corona. Can any one share or explain me the code to make the text field user input to get saved in the particular row in sqlite database?

Looking forward for help.

Thanks in advance.

We have sample apps that show individual features.  For instance in the SampleCode/Storage/SQLite folder is an example of how to create and save data into an SQLite database.  In the SampleCode/Interface/NativeKeyboard sample you will learn how to show the keyboard and collect input from it.

Merge the techniques from both to start your app.

Rob

Hi Rob,

Thanks for the suggestion, i have got little success. Will get back you if i encounter any difficulties on this.

Regards

Hi,

Need your help i am able to get the user input now from the text field and update it to the db, but i am not able to refresh and display the update instantly on the page need to close and relaunch the app to see the update here is the code…

local path = system.pathForFile(“data.db”, system.DocumentsDirectory)

db = sqlite3.open(path)   

–Handle the applicationExit event to close the db

local function onSystemEvent( event )

        if( event.type == “applicationExit” ) then              

            db:close()

        end

end

–Setup the table if it doesn’t exist

local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]

print(tablesetup)

db:exec( tablesetup )

textField = native.newTextField( 30, 190, 270, 35 )

textField:setReferencePoint( display.BottomLeftReferencePoint )

textField.size = 16

textField.inputType = “text”

screenGroup:insert(textField)

   saveData = function ( event )

       textString = textField.text

    --textString = “Terminator”    

        local testvalue = {}

    

    testvalue[1] = ‘’

    testvalue[2] = ‘World’

    testvalue[3] = ‘Status updated as:’

    

    local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[3]…[[’,’]]…textString…[[’);]]    

    local tablefill1 =[[UPDATE test SET content2 =’]] …textString…[[’ WHERE id = 1;]]

    local tablefill3 =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[3]…[[’);]]

    --db:exec( tablefill )

    db:exec( tablefill1 )

    --db:exec( tablefill3 )

end

        savebutton = widget.newButton {

        left = 60,

        top = 250,

        default = “buttonGreen.png”,

        over = “buttonGreenOver.png”,

        label = “Update”,

        embose = true,

        onRelease = saveData

        }

     

–print the sqlite version to the terminal

print( "version " … sqlite3.version() )

–print all the table contents

for row in db:nrows(“SELECT * FROM test”) do

  local text = row.content…" "…row.content2

  local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)

  t:setFillColor(255,0,0)

end

–setup the system listener to catch applicationExit

Runtime:addEventListener( “system”, onSystemEvent )

end

Please help me with that so that I can see my update instantly on the screen after pressing the update button,

Hi Rob found the solution for instant update.

We have sample apps that show individual features.  For instance in the SampleCode/Storage/SQLite folder is an example of how to create and save data into an SQLite database.  In the SampleCode/Interface/NativeKeyboard sample you will learn how to show the keyboard and collect input from it.

Merge the techniques from both to start your app.

Rob

Hi Rob,

Thanks for the suggestion, i have got little success. Will get back you if i encounter any difficulties on this.

Regards

Hi,

Need your help i am able to get the user input now from the text field and update it to the db, but i am not able to refresh and display the update instantly on the page need to close and relaunch the app to see the update here is the code…

local path = system.pathForFile(“data.db”, system.DocumentsDirectory)

db = sqlite3.open(path)   

–Handle the applicationExit event to close the db

local function onSystemEvent( event )

        if( event.type == “applicationExit” ) then              

            db:close()

        end

end

–Setup the table if it doesn’t exist

local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, content, content2);]]

print(tablesetup)

db:exec( tablesetup )

textField = native.newTextField( 30, 190, 270, 35 )

textField:setReferencePoint( display.BottomLeftReferencePoint )

textField.size = 16

textField.inputType = “text”

screenGroup:insert(textField)

   saveData = function ( event )

       textString = textField.text

    --textString = “Terminator”    

        local testvalue = {}

    

    testvalue[1] = ‘’

    testvalue[2] = ‘World’

    testvalue[3] = ‘Status updated as:’

    

    local tablefill =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[3]…[[’,’]]…textString…[[’);]]    

    local tablefill1 =[[UPDATE test SET content2 =’]] …textString…[[’ WHERE id = 1;]]

    local tablefill3 =[[INSERT INTO test VALUES (NULL, ‘]]…testvalue[1]…[[’,’]]…testvalue[3]…[[’);]]

    --db:exec( tablefill )

    db:exec( tablefill1 )

    --db:exec( tablefill3 )

end

        savebutton = widget.newButton {

        left = 60,

        top = 250,

        default = “buttonGreen.png”,

        over = “buttonGreenOver.png”,

        label = “Update”,

        embose = true,

        onRelease = saveData

        }

     

–print the sqlite version to the terminal

print( "version " … sqlite3.version() )

–print all the table contents

for row in db:nrows(“SELECT * FROM test”) do

  local text = row.content…" "…row.content2

  local t = display.newText(text, 20, 120 + (20 * row.id), native.systemFont, 16)

  t:setFillColor(255,0,0)

end

–setup the system listener to catch applicationExit

Runtime:addEventListener( “system”, onSystemEvent )

end

Please help me with that so that I can see my update instantly on the screen after pressing the update button,

Hi Rob found the solution for instant update.