sqlite on file system

Hi,

I’m trying to create an sqlite database that will live on the file system so I can get it across sessions, but I can’t figure out how.

Doing something like:
local db = sqlite3.open(‘httt.sqlite3’);

works fine in the emulator but doesn’t work on the device (tested on iPod Touch). I tried using stuff like createpath but those didn’t seem to work. The documentation only shows how to open/create an in memory database.

Any insight is appreciated.

Regards,
~David [import]uid: 6678 topic_id: 1274 reply_id: 301274[/import]

Also, just to clarify, when I do something like this:
local path = system.pathForFile( “httt.sqlite3”, system.ResourceDirectory )
local db = sqlite3.open(path);

I get the following error in the simulator terminal:
Runtime error
/Projects/myprojectname/main.lua:54: bad argument #1 to ‘open’ (string expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘open’
[import]uid: 6678 topic_id: 1274 reply_id: 3429[/import]

You can’t create new files in the “system.ResourceDirectory” …

Did you try it with the “system.DocumentsDirectory”?

[import]uid: 6928 topic_id: 1274 reply_id: 3434[/import]

Thanks, I think that was part of the problem, but to get it to work I had to end up doing this:

local path = system.pathForFile( “httt.sqlite3”, system.DocumentsDirectory )
local file = io.open( path, “r” )
if file then
io.close( file );
else
file = io.open( path, “w” )
io.close( file )
end
path = system.pathForFile( “httt.sqlite3”, system.DocumentsDirectory )
local db = sqlite3.open(path);

because otherwise you initially send sqlite3.open() an invalid, nil file name. [import]uid: 6678 topic_id: 1274 reply_id: 3439[/import]

@DFox

I think you got fooled by mixing things up… really!

local sqlite3 = require 'sqlite3'  
local path = system.pathForFile( "scores.sqlite3", system.DocumentsDirectory )  
local db = sqlite3.open(path)  

Works as expected in the Simulator and on the Device

It creates the file if it not already exists and opens it for access.

I just tested it with my devices… [import]uid: 6928 topic_id: 1274 reply_id: 3441[/import]

Hmm, thanks, I could have sworn I tried that, but I’ll give it another try a little later! I’ll let you know what happens. [import]uid: 6678 topic_id: 1274 reply_id: 3451[/import]