SQLiteDatabase docs example does not works

The code below does not works:

Var dbFile As New FolderItem("MyDB.sqlite")

Var db As New SQLiteDatabase
db.DatabaseFile = dbFile

'db.EncryptionKey = "horse+20$"

Try
  db.Connect
  ' Key was correct; DB is connected
  MessageBox("Connected to database.")
Catch error As DatabaseException
  ' Connection error. This could be because the key is wrong or other reasons
  MessageBox("Connection error: " + error.Message)
End Try

It comes from the documentation.

I commented the db.EncryptionKey line because this is not the cause, so, please, do not ask.

The error arise at db.Connect.

This code is self contained: put it in a button as is.

Xojo 2024r1
Sonoma 14.4.1
MacBook Pro m1

Do you have a copy files build step that puts the database next to the app?

Can you elaborate a bit, I do not understand (not sure).

I checked the FolderItem entry in the Documentation, and nothing about that kind of line.

Imagine a newbie in that situation.

NB: the shared code is the only code in the project.

The exception says:

Database file doesn’t exist in the specified location.

So this should work:

Var dbFile As New FolderItem("MyDB.sqlite")

Var db As New SQLiteDatabase
db.DatabaseFile = dbFile

db.EncryptionKey = "horse+20$"

Try
  If Not dbFile.Exists Then
    db.CreateDatabase
  End If
  
  db.Connect
  ' Key was correct; DB is connected
  MessageBox("Connected to database.")
Catch error As DatabaseException
  ' Connection error. This could be because the key is wrong or other reasons
  MessageBox("Connection error: " + error.Message)
End Try

That code says before it:

This code connect to an existing SQLite database:

So you must create it before attempt to connect DB.

Where do you read that ?

NB: it was the conclusion I get, but I checked the documentation and I do not read something telling that.

Here: SQLiteDatabase — Xojo documentation

To encrypt a new database, specify this value before calling CreateDatabase.
To connect to an encrypted database, specify this value before calling Database.Connect.

If you don’t use encryption, maybe you will find this code more helpful: SQLiteDatabase — Xojo documentation

There are also some SQLite example projects included:

1 Like