Why SQLite database can't be opened at times?

Hello,

In every scene I am opening the SQLite database at the beginning and then doing a DB:Close() when I exit the scene. Every once in a while I get a message that I was attempting to use a closed SQLite database. Has anyone else had this happen? Should I do a check when I try to open the database to verify it is open and if not try again? My question is how can I tell if it opened successfully or not?

I open the database like this:

local path = system.pathForFile("database.db", system.DocumentsDirectory) db = sqlite3.open( path ) 

Do I just check if db is nil? If so I can do that but still strange it only happens once in a while and this is in my windows simulator.

Thanks,

Warren

Maybe just open it once? I only open my DB on start and close it on “applicationExit” event. No problems using that method.

Really? I thought I would have to open it in every scene with the composer. I unload each scene as I am finished with each. If that will work I will give it a try today.

Thanks

You can make a DB module for example and require it when you need to do something.

[lua]-- Db.lua

local M = {}

local path = system.pathForFile(“database.db”, system.DocumentsDirectory)

M.db = sqlite3.open( path )

function M.closeDb()

   – my close db function

end

return M[/lua]

Then in a scene something like:

[lua]local database = require(“Db”)

local db = database.db

[/lua]

I just tried only opening the db on the first scene and then using it on the next and it gave an error attempt to index upvalue ‘db’ (a nil value). So I can’t do it this way right?

The module you gave above is mainly to call from one place and open each time right? Because I have the code to open it at the top of each scene it is used in and close the db when I call a function to remove all objects from the scene.

Thanks

The great thing about requiring a Lua file the code is only executed once. 

You can test by putting print(“Hello there”) at the top of the file, its only printed the first time you do require.

Maybe just open it once? I only open my DB on start and close it on “applicationExit” event. No problems using that method.

Really? I thought I would have to open it in every scene with the composer. I unload each scene as I am finished with each. If that will work I will give it a try today.

Thanks

You can make a DB module for example and require it when you need to do something.

[lua]-- Db.lua

local M = {}

local path = system.pathForFile(“database.db”, system.DocumentsDirectory)

M.db = sqlite3.open( path )

function M.closeDb()

   – my close db function

end

return M[/lua]

Then in a scene something like:

[lua]local database = require(“Db”)

local db = database.db

[/lua]

I just tried only opening the db on the first scene and then using it on the next and it gave an error attempt to index upvalue ‘db’ (a nil value). So I can’t do it this way right?

The module you gave above is mainly to call from one place and open each time right? Because I have the code to open it at the top of each scene it is used in and close the db when I call a function to remove all objects from the scene.

Thanks

The great thing about requiring a Lua file the code is only executed once. 

You can test by putting print(“Hello there”) at the top of the file, its only printed the first time you do require.