HELP ME. I'M CONFUSED WITH THE DATABASE. I'M STUCKED

I have this program here in which i’m making a grocery android app. My buttons for incrementing and decrementing were okay. It’s working. As i’m clicking the button the textPurchase is changing. But my problem is, when I go to the another composer and goes back again, the buttons were still working but the textPurchase.text is not changing anymore. It is stuck.

Here is my code for the composer I’m working into…


-------- Meats _ 1


local composer = require( “composer” )

local scene = composer.newScene()

local widget = require “widget” --required in order to make a button

local sqlite3 = require (“sqlite3”)

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

local db = sqlite3.open( path ) 


-------- BEGINNING OF THE IMPLEMENTATION


local function onbackBtnRelease()

composer.gotoScene(“home2”, “slideRight”, 100)

return true

end

local function onnextBtnRelease()

composer.gotoScene(“meat2”, “slideLeft”, 100)

return true

end

–Increment and Decrement of Daing

local function onLDaingBtnRelease()

if NUMdaing ~= 0 then

NUMdaing = NUMdaing - 1

txtNUMdaing.text = NUMdaing

print(NUMdaing)

coin = coin - 97.00

txtPurchase.text = coin

print (coin)

end

end

local function onRDaingBtnRelease()

NUMdaing = NUMdaing + 1

txtNUMdaing.text = NUMdaing

print(NUMdaing)

coin = coin + 97.00

txtPurchase.text = coin

print (coin)

end

–Increment and Decrement of pigue

local function onLpigueBtnRelease()

if NUMpigue == 0 then

else

NUMpigue = NUMpigue - 1

txtNUMpigue.text = NUMpigue

print(NUMpigue)

coin = coin - 201

txtPurchase.text = coin

print (coin)

end

end

local function onRpigueBtnRelease()

NUMpigue = NUMpigue + 1

txtNUMpigue.text = NUMpigue

print(NUMpigue)

coin = coin + 201

txtPurchase.text = coin

print (coin)

end

–Increment and Decrement of porkchop

local function onLporkchopBtnRelease()

if NUMporkchop == 0 then

else

NUMporkchop = NUMporkchop - 1

txtNUMporkchop.text = NUMporkchop

print(NUMporkchop)

coin = coin - 220

txtPurchase.text = coin

print (coin)

end

end

local function onRporkchopBtnRelease()

NUMporkchop = NUMporkchop + 1

txtNUMporkchop.text = NUMporkchop

print(NUMporkchop)

coin = coin + 220

txtPurchase.text = coin

print (coin)

end

-----------------------------CREATE SCENE---------------------------

function scene:create(event)

local sceneGroup = self.view

–background

local background = display.newImage( “bgmenu.png”)

background:scale(1.0,1.0)

background:translate(165,640) --x and y axis

sceneGroup:insert(background)

–Back Button

local backBtn = widget.newButton

{

x = 40,

y = 3,

width = 40,

height = 30,

defaultFile = “back.png”,

overFile = “back.png”,

onRelease = onbackBtnRelease,

}

sceneGroup:insert(backBtn)

–Meats title

local meatsTitle = display.newImage(“meatsTitle.png”)

meatsTitle:scale(0.4,0.4)

meatsTitle:translate(165,1) --x and y axis

sceneGroup:insert(meatsTitle)

–Next Button

local nextBtn = widget.newButton

{

x = 289,

y = 3,

width = 40,

height = 30,

defaultFile = “next.png”,

overFile = “next.png”,

onRelease = onnextBtnRelease,

}

sceneGroup:insert(nextBtn)

-------------------------PRODUCTS------------------------

-------*DAING BANGUS*-------

local daing = display.newImage( “daing.jpg”)

daing:scale(.3,.3)

daing:translate(60,120) --x and y axis

sceneGroup:insert(daing)

–daing Text

local daingText = {

    text = “Daing Bangus”,     

    x = 190,

    y = 70,

    font = “Times New Roman”,

    fontSize = 35

}

–displaying/printing daing

txtdaing = display.newText( daingText )

txtdaing:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtdaing)

–details of daing

local GdaingText = {

    text = “510g = ₱97.00”,     

    x = 190,

    y = 110,

    font = “Times New Roman”,

    fontSize = 20

}

txtGdaing = display.newText( GdaingText )

txtGdaing:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtGdaing)

–buttons of daing

local LdaingBtn = widget.newButton

{

x = 270,

y = 110,

width = 30,

height = 30,

defaultFile = “left.png”,

overFile = “left.png”,

onRelease = onLDaingBtnRelease,

}

sceneGroup:insert(LdaingBtn)

for row in db:nrows(“SELECT NUMdaing FROM NUMdaings WHERE name = 0”) do

  NUMdaing = row.NUMdaing

end

local NUMdaingText = {    

text = NUMdaing,

    x = 290,

    y = 110,

    font = “Times New Roman”,

    fontSize = 30

}

txtNUMdaing = display.newText( NUMdaingText )

txtNUMdaing:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtNUMdaing)

local RdaingBtn = widget.newButton

{

x = 310,

y = 110,

width = 30,

height = 30,

defaultFile = “right.png”,

overFile = “right.png”,

onRelease = onRDaingBtnRelease,

}

sceneGroup:insert(RdaingBtn)

–line

local line = display.newImage(“line.jpg”)

line:scale(0.55,0.5)

line:translate(155,180) --x and y axis

sceneGroup:insert(line)

-------*PIGUE*-------

local pigue = display.newImage( “pigue.jpg”)

pigue:scale(.4,.4)

pigue:translate(60,260) --x and y axis

sceneGroup:insert(pigue)

–pigue Text

local pigueText = {

    text = “Pigue”,     

    x = 140,

    y = 210,

    font = “Times New Roman”,

    fontSize = 35

}

–displaying/printing pigue

txtpigue = display.newText( pigueText )

txtpigue:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtpigue)

– details of Pingue

local KGpigueText = {

    text = “1kg = ₱201.00”,     

    x = 190,

    y = 250,

    font = “Times New Roman”,

    fontSize = 20

}

txtKGpigue = display.newText( KGpigueText )

txtKGpigue:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtKGpigue)

–buttons of pigue

local LpigueBtn = widget.newButton

{

x = 270,

y = 250,

width = 30,

height = 30,

defaultFile = “left.png”,

overFile = “left.png”,

onRelease = onLpigueBtnRelease,

}

sceneGroup:insert(LpigueBtn)

NUMpigue = 0

local NUMpigueText = {    

text = NUMpigue,

    x = 290,

    y = 250,

    font = “Times New Roman”,

    fontSize = 30

}

txtNUMpigue = display.newText( NUMpigueText )

txtNUMpigue:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtNUMpigue)

local RpigueBtn = widget.newButton

{

x = 310,

y = 250,

width = 30,

height = 30,

defaultFile = “right.png”,

overFile = “right.png”,

onRelease = onRpigueBtnRelease,

}

sceneGroup:insert(RpigueBtn)

–line

local line = display.newImage(“line.jpg”)

line:scale(0.55,0.5)

line:translate(155,330) --x and y axis

sceneGroup:insert(line)

-------*PORK CHOP*-------

local porkchop = display.newImage( “porkchop.jpg”)

porkchop:scale(.4,.4)

porkchop:translate(50,400) --x and y axis

sceneGroup:insert(porkchop)

–porkchop Text

local porkchopText = {

    text = “Pork Chop”,     

    x = 180,

    y = 360,

    font = “Times New Roman”,

    fontSize = 35

}

–displaying/printing porkchop

txtporkchop = display.newText( porkchopText )

txtporkchop:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtporkchop)

–details of porkchop

local porkchopKGText = {

    text = “1kg = ₱220.00”,     

    x = 190, 

    y = 400,

    font = “Times New Roman”,

    fontSize = 20

}

txtKGporkchop = display.newText( porkchopKGText )

txtKGporkchop:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtKGporkchop)

–buttons of porkchop

local LporkchopBtn = widget.newButton

{

x = 270,

y = 400,

width = 30,

height = 30,

defaultFile = “left.png”,

overFile = “left.png”,

onRelease = onLporkchopBtnRelease,

}

sceneGroup:insert(LporkchopBtn)

NUMporkchop = 0

local NUMporkchopText = {    

text = NUMporkchop,

    x = 290,

    y = 400,

    font = “Times New Roman”,

    fontSize = 30

}

txtNUMporkchop = display.newText( NUMporkchopText )

txtNUMporkchop:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtNUMporkchop)

local RporkchopBtn = widget.newButton

{

x = 310,

y = 400,

width = 30,

height = 30,

defaultFile = “right.png”,

overFile = “right.png”,

onRelease = onRporkchopBtnRelease,

}

sceneGroup:insert(RporkchopBtn)

-------------------------END OF PRODUCTS------------------------

–line

local line = display.newImage(“line.jpg”)

line:scale(0.55,0.5)

line:translate(155,472) --x and y axis

sceneGroup:insert(line)

–cart

local cart = display.newImage(“cart.png”)

cart:scale(0.15,0.15)

cart:translate(40,495) --x and y axis

sceneGroup:insert(cart)

–peso sign

local peso = display.newImage(“peso.png”)

peso:scale(0.025,0.025)

peso:translate(85,500) --x and y axis

sceneGroup:insert(peso)

for row in db:nrows(“SELECT coin FROM coins WHERE name = 0”) do

  coin = row.coin

end

–Purchase Text

local optionsText = {

    text = coin,     

    x = 140,

    y = 500,

    font = “Times New Roman”,

    fontSize = 30

}

–displaying/printing Purchase

txtPurchase = display.newText( optionsText )

txtPurchase:setFillColor( 0, 0, 0 )

sceneGroup:insert(txtPurchase)

–checkout Button

local checkoutBtn = widget.newButton

{

x = 250,

y = 500,

width = 100,

height = 30,

defaultFile = “checkout.png”,

overFile = “checkout2.png”,

–onRelease = oncheckBtnRelease,

}

sceneGroup:insert(checkoutBtn)

local function onSystemEvent( event )

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

        if ( db and db:isopen() ) then

            db:close()

        end

    end

end

Runtime:addEventListener( “system”, onSystemEvent )

end

-----------------------------SHOW SCENE------------------------------

function scene:show(event)

local sceneGroup = self.view

end

-----------------------------HIDE SCENE------------------------------

function scene:hide( event )

    local sceneGroup = self.view

   

end

-----------------------------DESTROY SCENE---------------------------

function scene:destroy( event )

    local sceneGroup = self.view

    – Called prior to the removal of scene’s view (“sceneGroup”).

    – Insert code here to clean up the scene.

    – Example: remove display objects, save state, etc.

    

   

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

and this is my main.lua


-------- MAIN LUA


– include Corona’s “widget” library

display.setStatusBar( display.HiddenStatusBar )               – hide the status bar

local composer = require (“composer”)

composer.gotoScene(“startBtn”, “fade”, 1000)

require “sqlite3”

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

db = sqlite3.open(path)

local tablesetup = “CREATE TABLE IF NOT EXISTS coins (name INTEGER PRIMARY KEY, coin INTEGER);”

db:exec(tablesetup )

local tablesetup = “CREATE TABLE IF NOT EXISTS NUMdaings (name INTEGER PRIMARY KEY, NUMdaing INTEGER);”

db:exec(tablesetup )

– COINS DATA – 

local tablefill = “INSERT INTO coins VALUES(0,0);”

db:exec(tablefill)

local tablefill = “INSERT INTO NUMdaings VALUES(0,0);”

db:exec(tablefill)

local function onSystemEvent(event)

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

db:close()

end

end

Runtime:addEventListener(“system”, onSystemEvent)

Hi @rinkimi_13. Welcome to the Corona Labs forums. You are likely not going to get any useful responses to your question for a couple of reasons (which you can fix).

First, your code is unformatted, making it near impossible to read. When posting code to the forums, please click on the blue <> button in the edit bar with Bold, Italic, etc. This will popup a windows that you can paste your code into and it will format it in a more readable way.

Next you dumped way more code that you probably needed to. A better description of the problem and the **related** code will be easier to digest. It’s always best to start out with a small amount of relevant code and wait until someone asks to see more. 

If you could go back and edit your post, cut the amount of code down, make sure its formatted and then you will likely get responses. Also please read this post before you continue:  https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Thanks

Rob

Hi @rinkimi_13. Welcome to the Corona Labs forums. You are likely not going to get any useful responses to your question for a couple of reasons (which you can fix).

First, your code is unformatted, making it near impossible to read. When posting code to the forums, please click on the blue <> button in the edit bar with Bold, Italic, etc. This will popup a windows that you can paste your code into and it will format it in a more readable way.

Next you dumped way more code that you probably needed to. A better description of the problem and the **related** code will be easier to digest. It’s always best to start out with a small amount of relevant code and wait until someone asks to see more. 

If you could go back and edit your post, cut the amount of code down, make sure its formatted and then you will likely get responses. Also please read this post before you continue:  https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Thanks

Rob

Where in your code do you increment and decrement the textPurchase?

Where in your code do you increment and decrement the textPurchase?