I am making an interactive children’s book using SQLite to store the text strings and the names of image/audio files. Since I will be using the database on every page I’m wondering if I should open & close it on each page or if this should be handled in the main.lua file leaving the database open the whole time. Thank you!
P.S. This is what I have working right now. I am a newbie here so would also appreciate any suggestions.
[lua]----------------------------------------------------------------------------------
– scenetemplate.lua
–include squlite
require “sqlite3”
–open database set path to the data folder inside our project
local path = system.pathForFile(“myDB.sqlite”, system.ResourceDirectory)
db = sqlite3.open ( path )
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local txtA
local imgA
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
local text
print ("version "…sqlite3.version())
print ("db path "…path)
local sql = “SELECT myText FROM bookText WHERE page = ‘page1’ LIMIT 1”
for row in db:nrows(sql) do
text = row.myText
end
imgA = display.newImage ( “images/letterA.png” )
imgA:setReferencePoint( display.CenterReferencePoint )
imgA.x = display.contentWidth * 0.25
imgA.y = display.contentHeight * 0.5
txtA = display.newText(text, display.contentWidth * 0.6, display.contentHeight * 0.1, display.contentWidth * 0.35, display.contentHeight * 0.8, native.systemFont, 60)
group:insert( imgA )
group:insert( txtA )
end
– Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
local group = self.view
– This event requires build 2012.782 or later.
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
– system listener for application exit
Runtime:addEventListener (“system”, onSystemEvent)
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
– handle the applicationExit event to close the db
local function onSystemEvent( event )
if(event.type == “applicationExit”) then
db:close()
end
end
– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
end
– Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
local group = self.view
– This event requires build 2012.782 or later.
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
– INSERT code here (e.g. remove listeners, widgets, save state, etc.)
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “willEnterScene” event is dispatched before scene transition begins
scene:addEventListener( “willEnterScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “didExitScene” event is dispatched after scene has finished transitioning out
scene:addEventListener( “didExitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
[/lua] [import]uid: 117117 topic_id: 33872 reply_id: 333872[/import]