Storyboard + SQLite

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]

Hi @Egret,
I would definitely suggest you access your database just once (at least to get its core contents) in main.lua, instead of opening and closing it every scene. Remember that you can easily pass variables/tables to and from Storyboard scenes, so in theory you could just place your data into a nicely-structured table and pass that along from scene to scene, accessing whatever you need for each scene.

Best regards,
Brent
[import]uid: 200026 topic_id: 33872 reply_id: 134639[/import]

Thank you Brent. So I think what you are recommending is to open the database in the main.lua, grab the values and dump them into a lua table, close the database, then call the storyboard.gotoScene event. Now I should have a table to pass to different storyboard scenes, but I’m not sure how to do that. [import]uid: 117117 topic_id: 33872 reply_id: 134671[/import]

I’m going to agree with Brent on open it once. But if you’re going to read the whole database in at once, why not just build a .lua file that is the table of info that you need. A few of good questions to ask yourself if you should use a database or not are these:

Do I need to only fetch a few records (or one) based on some criteria?
Will I be updating the data as the app runs?
Am I dealing with a large set of data that may grow over time?

If so, a database is a good choice. If you need fixed data that you’re going to have in memory all the time and you are not updating it, then a hand coded table from a fixed .lua file will do what you want.

Need something in between? There are several good and simple modules in the Community code for Saving a Table to the Documents directory and reading it back in.
[import]uid: 199310 topic_id: 33872 reply_id: 134701[/import]

And just to follow up, @Egret, if you decide to build an external Lua module with the data (instead of a local database), you can “require” it in easily on each Storyboard scene, so you don’t have to pass it from scene to scene. If you need an example (in code) of how to do that, let me know and I’ll show you a quick-n-dirty example.

Brent
[import]uid: 200026 topic_id: 33872 reply_id: 134709[/import]

Thank you for taking the time to respond.

Brent, I have already searched the require() function and have it working. I’m glad I read up on it.

Rob, I haven’t yet had the chance to look into the Documents Directory, but plan to this weekend.

I’m wondering which option you think would be best for my projects, as I am trying to find the best way to keep track of hundreds of audio and image files. I like the idea of organizing everything in excel then dumping it into a .csv file, but not if it is going to result in diminished performance for the end user. [import]uid: 117117 topic_id: 33872 reply_id: 134882[/import]

Hi @Egret,
I would definitely suggest you access your database just once (at least to get its core contents) in main.lua, instead of opening and closing it every scene. Remember that you can easily pass variables/tables to and from Storyboard scenes, so in theory you could just place your data into a nicely-structured table and pass that along from scene to scene, accessing whatever you need for each scene.

Best regards,
Brent
[import]uid: 200026 topic_id: 33872 reply_id: 134639[/import]

There are a couple of things you can do if you want to use CSV files. The first requires a bit of code, the other a little less.

  1. Write a CSV parser. This may sound scary, but a community member already has. See this forum post:
    http://developer.coronalabs.com/forum/2011/07/22/getting-csv-list-file-array

You should be able to expand on that and make it work.

The other option involves converting your CSV into JSON. When people first look at trying to figure out JSON, they tend to run away because trying to figure it out is a pain. But Corona/Lua has a great library that reads JSON data and turns it into a Lua table in one line of code (well you still have to write the code to read the JSON file). There is also one line of code to turn a Lua table into a JSON data string that can be easily written out to storage. You could use a service like this:

http://www.cparker15.com/code/utilities/csv-to-json/

that will turn your CSV into JSON then you could could use a couple of the community provided load/save table functions in the community code.
[import]uid: 199310 topic_id: 33872 reply_id: 134908[/import]

Thank you Brent. So I think what you are recommending is to open the database in the main.lua, grab the values and dump them into a lua table, close the database, then call the storyboard.gotoScene event. Now I should have a table to pass to different storyboard scenes, but I’m not sure how to do that. [import]uid: 117117 topic_id: 33872 reply_id: 134671[/import]

I’m going to agree with Brent on open it once. But if you’re going to read the whole database in at once, why not just build a .lua file that is the table of info that you need. A few of good questions to ask yourself if you should use a database or not are these:

Do I need to only fetch a few records (or one) based on some criteria?
Will I be updating the data as the app runs?
Am I dealing with a large set of data that may grow over time?

If so, a database is a good choice. If you need fixed data that you’re going to have in memory all the time and you are not updating it, then a hand coded table from a fixed .lua file will do what you want.

Need something in between? There are several good and simple modules in the Community code for Saving a Table to the Documents directory and reading it back in.
[import]uid: 199310 topic_id: 33872 reply_id: 134701[/import]

And just to follow up, @Egret, if you decide to build an external Lua module with the data (instead of a local database), you can “require” it in easily on each Storyboard scene, so you don’t have to pass it from scene to scene. If you need an example (in code) of how to do that, let me know and I’ll show you a quick-n-dirty example.

Brent
[import]uid: 200026 topic_id: 33872 reply_id: 134709[/import]

Thank you for taking the time to respond.

Brent, I have already searched the require() function and have it working. I’m glad I read up on it.

Rob, I haven’t yet had the chance to look into the Documents Directory, but plan to this weekend.

I’m wondering which option you think would be best for my projects, as I am trying to find the best way to keep track of hundreds of audio and image files. I like the idea of organizing everything in excel then dumping it into a .csv file, but not if it is going to result in diminished performance for the end user. [import]uid: 117117 topic_id: 33872 reply_id: 134882[/import]

There are a couple of things you can do if you want to use CSV files. The first requires a bit of code, the other a little less.

  1. Write a CSV parser. This may sound scary, but a community member already has. See this forum post:
    http://developer.coronalabs.com/forum/2011/07/22/getting-csv-list-file-array

You should be able to expand on that and make it work.

The other option involves converting your CSV into JSON. When people first look at trying to figure out JSON, they tend to run away because trying to figure it out is a pain. But Corona/Lua has a great library that reads JSON data and turns it into a Lua table in one line of code (well you still have to write the code to read the JSON file). There is also one line of code to turn a Lua table into a JSON data string that can be easily written out to storage. You could use a service like this:

http://www.cparker15.com/code/utilities/csv-to-json/

that will turn your CSV into JSON then you could could use a couple of the community provided load/save table functions in the community code.
[import]uid: 199310 topic_id: 33872 reply_id: 134908[/import]

Thanks again, this really helps a lot. [import]uid: 117117 topic_id: 33872 reply_id: 135273[/import]

Thanks again, this really helps a lot. [import]uid: 117117 topic_id: 33872 reply_id: 135273[/import]