Opening sqlite db

I have a sqlite db in a folder on my desktop (same folder as the main.lua file).

In order to open this db, I was trying to use:

local path = system.pathForFile (“etuc.db”)

since I wanted this to work regardless of where the file is actually located, and not fixed to system.DocumentsDirectory.

This does not find anything, so what am I doing wrong?

Thanks.

That looks fine to me as a way to get the path to your database file if it’s in the same folder as your main.lua file (which is called the resource directory).

When you say it didn’t find anything, how do you know?  Do you mean that path is nil?

  • Andrew

I get a run time error when I try this in the Corona Simulator.

 

File: /Users/seandzafovic/Desktop/ETUC_Folder/main.lua

Line: 4

Bad argument #1 to ‘open’ (string expected, got nil)

stack traceback:

    [C]: ?

    [C]: in function ‘open’

    /Users/seandzafovic/Desktop/ETUC_Folder/main.lua:4: in main chunk

 

I thought if I did this in main.lua I could find out if the simulator is able to find the path to my db file.

 

 

[lua]

require “sqlite3”

local path = system.pathForFile (“etuc.db”)

local db = sqlite3.open( path )

if db then

    print (“db exists”)

end

local function onSystemEvent( event )

        if( event.type == “applicationExit” ) then              

            db:close()

        end

end

[/lua]

Hi,

So the problem is that system.pathForFile isn’t finding your file for some reason, which causes path to be nil.  When you then try sqlite3.open(path), you’re getting an error, since path is nil.  The right way to test it would be

[lua]

if path then db = sqlite3.open(path) end

[/lua]

Now the question is why system.pathForFile() isn’t finding your file.  Is etuc.db in the same exact folder as main.lua (not a sub-folder)?

  • Andrew

Well, figured out why the file was not found. I had called it “etuc” not “etuc.db”. No more runtime errors when I change the code to reflect this.

Glad you found the solution.

  • Andrew

That looks fine to me as a way to get the path to your database file if it’s in the same folder as your main.lua file (which is called the resource directory).

When you say it didn’t find anything, how do you know?  Do you mean that path is nil?

  • Andrew

I get a run time error when I try this in the Corona Simulator.

 

File: /Users/seandzafovic/Desktop/ETUC_Folder/main.lua

Line: 4

Bad argument #1 to ‘open’ (string expected, got nil)

stack traceback:

    [C]: ?

    [C]: in function ‘open’

    /Users/seandzafovic/Desktop/ETUC_Folder/main.lua:4: in main chunk

 

I thought if I did this in main.lua I could find out if the simulator is able to find the path to my db file.

 

 

[lua]

require “sqlite3”

local path = system.pathForFile (“etuc.db”)

local db = sqlite3.open( path )

if db then

    print (“db exists”)

end

local function onSystemEvent( event )

        if( event.type == “applicationExit” ) then              

            db:close()

        end

end

[/lua]

Hi,

So the problem is that system.pathForFile isn’t finding your file for some reason, which causes path to be nil.  When you then try sqlite3.open(path), you’re getting an error, since path is nil.  The right way to test it would be

[lua]

if path then db = sqlite3.open(path) end

[/lua]

Now the question is why system.pathForFile() isn’t finding your file.  Is etuc.db in the same exact folder as main.lua (not a sub-folder)?

  • Andrew

Well, figured out why the file was not found. I had called it “etuc” not “etuc.db”. No more runtime errors when I change the code to reflect this.

Glad you found the solution.

  • Andrew