Opening sqlite database and leave opened


First I wanto to say that I am an inexperienced xojo programmer.

I’m testing xojo with sqlite on desktop platform on mac os.

I’d like to open asqlite database and leaves opened for further operations so as far I can understant I’ve created a property as sqlitedtabase and visibility public and a method to open the database.

the method open database is below

Dim dbFile As FolderItem
dbFile = SpecialFolder.ApplicationData.Child(“gestio.sqlite”)

Dim db As New SQLiteDatabase
db.DatabaseFile = dbFile

If db.Error Then
MsgBox("Error: " + db.ErrorMessage)
End If

BaseDades = db

The last line is the property BaseDades that I want to use in methods.

Unfortunately, when I want to use this property in otger methods I get and error “Datbase is not open”

What am I doing wrong ?

Thank you

Did you connect to the database?

For better experience change the database location to desktop dbFile = SpecialFolder.desktop.Child("gestio.sqlite") or in the Documents folder dbFile = SpecialFolder.documents.Child("gestio.sqlite") and move the file there.
and change the bellow part

If db.Error Then MsgBox("Error: " + db.ErrorMessage) End If

If DB.Connect Then MsgBox("Connected to " + dbFile.Name) Else MsgBox("Error: " + db.ErrorMessage) End If
And check if the property is Global…

If all the code you posted above is in the same method, then your database goes out of scope almost immediately

Dim db As New SQLiteDatabase

This must be a global property, the way you depict it, it is a local one

Sort of what Dave was alluding to, if BaseDades is defined as a global property, then use it directly instead of creating a new separate variable.

Thank you for your help.

BaseDades is declared as SQLiteDatabase in app properties

so method should be

Dim dbFile As FolderItem

BaseDades.DatabaseFile = GetFolderItem(“gestio.sqlite”) < --BaseDades is a property declared in properties

if BaseDades.Connect Then
MsgBox("Connected to : " + dbfile)
MsgBox("Error: " + BaseDades.ErrorMessage)
end if

Unfortunately I get this error on BaseDades.DatabaseFile = GetFolderItem(“gestio.sqlite”)

An exception of class NilObjectException was not handled. The application must shut don

but as far I now , I can’t handle this sentence.

In other hand, I code this method using local variables and at the end of the method assign values to BaseDades it works fine.

Is this file (gestio.sqlite) Exists where you suppose it is located ?

Two things:

Add this statement before assigning the file:

BaseDades = New SQLiteDatabase

Then use dbFile to open the file and check that it exists with something like this:

dbFile = GetFolderItem("gestio.sqlite") If Not dbFile.Exists Then // handle error Else BaseDades.DatabaseFile = dbFile End

Try this

[code]Dim dbFile As FolderItem

app.BaseDades.DatabaseFile = GetFolderItem(“gestio.sqlite”) < --BaseDades is a property declared in properties

if app.BaseDades.Connect Then
MsgBox("Connected to : " + dbfile)
MsgBox("Error: " + app.BaseDades.ErrorMessage)
end if