Skip to content

Instantly share code, notes, and snippets.

@coronarob
Last active August 29, 2015 14:21
Show Gist options
  • Save coronarob/b617b93dc268cdd907d2 to your computer and use it in GitHub Desktop.
Save coronarob/b617b93dc268cdd907d2 to your computer and use it in GitHub Desktop.
Sample for copying a database to a writable location.
local sqlite3 = require( "sqlite3" )
local dbfunc = require("copyDBto")
local filename = "data.db"
local baseDir = system.DocumentsDirectory
-- Open "data.db". If the file doesn't exist, it will be created
local path = system.pathForFile( filename, baseDir )
local doesExist = io.open(path, "r")
if not doesExist then
local result = dbfunc.copyDatabaseTo( "data.db", { filename = "data.db", baseDir = system.DocumentsDirectory } )
assert( result, "Database failed to copy. Check the logs.")
else
io.close( doesExist )
end
local db = sqlite3.open( path )
-- Handle the "applicationExit" event to close the database
local function onSystemEvent( event )
if ( event.type == "applicationExit" ) then
db:close()
end
end
-- Print the table contents
for row in db:nrows("SELECT * FROM highscores") do
local text = row.name .. " : " .. row.score
local t = display.newText( text, 120, 30*row.id, nil, 16 )
t:setFillColor( 1, 0, 1 )
end
-- Setup the event listener to catch "applicationExit"
Runtime:addEventListener( "system", onSystemEvent )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment