Hi,
An app I am designing right now focuses on financial management. As this involves inputting numbers, a database had to be used (in which I chose sqlite). However, I am new to this method of storing information and am struggling with inputting a value into the database. Currently, a text box which numericField is in the ‘income’ scene in which numbers can be typed in. Under this, there is a ‘done’ button. My aim is to get the number written in the text box to get put into the database when the done button is pressed. This is the code for my income scene currently:
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
--Set up the SQLite library (the database for storing information)
local sqlite3 = require( "sqlite3" )
--Create a file path for the database file "data.db"
local path = system.pathForFile( "data.db", system.DocumentsDirectory )
--Open the database for access
local db = sqlite3.open( path )
--Creates the table for income, 'income' is the table name, 'IncomeValue' is the column
local incomeTableSetup = [[CREATE TABLE IF NOT EXISTS income ( IncomeValue INTEGER PRIMARY KEY autoincrement );]]
db:exec( incomeTableSetup )
--When the value is written in the text box, then the done button is pressed, this function runs
local function insertIncome()
local insertQuery = [[INSERT INTO income VALUES ( NULL, "valuefromtextbox");]]
db:exec( insertQuery )
end
--If the menu button is pressed
local function gotoMenu()
composer.gotoScene( "menu" )
end
-------------------------
-- Scene event functions
-- create()
function scene:create(event)
local sceneGroup = self.view
--Displaying objects/scene
display.setDefault( scenegroup, "background", 1, 1, 1)-- Making background white
local banner = display.newRect( sceneGroup, 0, 0, 400, 80 )--Setting a blue banner at the top
banner:setFillColor(0, 0, 1)
banner.x = display.contentCenterX
banner.y = -80
local menuBackground = display.newRect( sceneGroup, 0, 0, 140, 40 )
menuBackground:setFillColor( 0, 1, 0 )
menuBackground.x = 100
menuBackground.y = 550
local menuText = display.newText( sceneGroup, "Menu", 100, 550, native.systemFont, 28)
menuText:setFillColor( 0, 0, 0 )
local incomeBanner = display.newText( sceneGroup, "Income", display.contentCenterX, -60, native.systemFont, 28)
local amountText = display.newText( sceneGroup, "Add Amount:", display.contentCenterX, 70, native.systemFont, 28 )
amountText:setFillColor( 0, 0, 0 )
local doneBackground = display.newRect( sceneGroup, 0, 0, 100, 40)
doneBackground:setFillColor( 0, 1, 0 )
doneBackground.x = display.contentCenterX
doneBackground.y = 220
local doneText = display.newText( sceneGroup, "Done", display.contentCenterX, 220, native.systemFont, 20 )
doneText:setFillColor( 0, 0, 0 )
local numericField = native.newTextField( display.contentCenterX, 150, 220, 36, sceneGroup )
numericField.inputType = "number"
menuBackground:addEventListener( "tap", gotoMenu )
doneBackground:addEventListener( "tap", insertIncome )
end
--Listeners for buttons
--show()
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif (phase == "did") then
end
end
--hide()
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif( phase == "did") then
composer.removeScene( "income")
end
end
scene:addEventListener( "create", scene)
scene:addEventListener( "show", scene)
scene:addEventListener( "hide", scene)
scene:addEventListener( "destroy", scene)
return scene
Any help would be much appreciated, thanks!