Read SQLite Encrypted from CopyFiles

Further to my previous post on encrypting App files I am following suggestions to put my Regex dictionaries into/out of encrypted SQLite files stored in my copy files.
I think I have been able to encrypt the files ok but can’t read them back. I have just been following the examples and have the following but can’t connect to the DB. Where could the issue be?

Var file As FolderItem = SpecialFolder.Resource("MyDictionary.sqlite")

If file <> Nil Then
  mDBSQLite = New SQLiteDatabase
  mDBSQLite.DatabaseFile = file
  mDBSQLite.EncryptionKey = "aes256:5uper_2ecurePa55m0r9!"
  If Not mDBSQLite.Connect Then
    MessageBox("Error connecting to the database")
  End If
Else
  MessageBox("Error found while creating the file")
End If

mDBSQLite.Decrypt

SQLExtractData  //extract the data and put back in Dictionaries

I don’t think you should be using the decrypt command as that removes the encryption which means you don’t require the encryption key next time.

At the moment I can’t read the SQLite file from copy files wether it is encrypted or not.

Two small things: there is a “s” missing at the end of “SpecialFolder.Resource” - it must be

SpecialFolder.Resources

And you only check if file <> Nil but not if it exists…

Adding the ‘s’ gives an error

Did you check whether the file has the sqlite database in the resources folder?
e.g. set a break point and check manually in Finder to see if it is there.

2 Likes

Sorry, that was maybe a litte bit to short…

Var file As FolderItem = SpecialFolder.Resources.Child("MyDictionary.sqlite")

If file <> Nil and file.Exists Then
  mDBSQLite = New SQLiteDatabase
  mDBSQLite.DatabaseFile = file
  mDBSQLite.EncryptionKey = "aes256:5uper_2ecurePa55m0r9!"
  If Not mDBSQLite.Connect Then
    MessageBox("Error connecting to the database")
  End If
Else
  MessageBox("Error found while creating the file")
End If

// mDBSQLite.Decrypt

SQLExtractData  //extract the data and put back in Dictionaries

This works also but wondering if it is deprecated
Var file As FolderItem = SpecialFolder.Resource(“backup.sqlite”)

I must admit, i’ve never seen that version of the syntax before.

I performed a quick check and both versions seem to point to the same path so either should have worked.

Var file As FolderItem = SpecialFolder.Resource("backup.sqlite")
Var file2 As FolderItem = SpecialFolder.Resources.Child("backup.sqlite")

If file.NativePath = file2.NativePath Then
  Break
End If
1 Like

Thanks Thomas, I removed the decrypt as suggested but rewrote a demo app which probably should have done from the start instead of trying to integrate it straight into the project but I now have it working :slight_smile:

Maybe my final question on this one. If the only purpose I have for the SQLite database is for encrypting the data how do I remove it from memory once I have extracted the data?
Does mydb.close or mydb = nil remove an in-memory database altogether?

It will release it, not exacly erase it. Will become inaccessible to your app, but if you may be thinking about “will the contents vanish?” the answer is no, the memory garbage will be left in the memory. With right tools and a huge mem dump one could find “traces” of the contents. But it should be too much work for the average user. After the close and Nil you could allocate a big membloc and release it too, I guess Xojo nullify new blocks and with luck it will use the same released area by the DB erasing many traces.

1 Like

Just be aware of issues with Databases in Resources… especially if you’re going to Sandbox the app and/or App Store is the ultimate target.

See for example: Option to open a RealSQL database as readonly (#21814) · Issues · Xojo Inc / Xojo · GitLab

1 Like

One should put only read-only contents in the resources. If they want read-write, should copy the content stored in the resources to a proper read/write user space (folder) before working on it.

1 Like

It looks (from the Issue) that we can’t have read-only connections to databases in Xojo?

1 Like

SQLite, currently, yep.

https://tracker.xojo.com/xojoinc/xojo/-/issues/21814

1 Like

Is it ok to put my encrypted sqlite files in the copyfiles? It seems to be working

Yes, it is. You just should not open them (read/write) THERE, in the resources folder.

2 Likes