Detect SQLite Encryption?

If I connect to a encrypted SQLite Databasefile without setting the encryption key first, then upon connection I’m getting an Errorcode 21 with this message: “LIBRARY ROUTINE CALLED OUT OF SEQUENCE”.

I wonder if there is a reliable way to catch errors and then have a meaningful message displayed to the user (the one above is not).

In a older thread on the NUG it says “… The ErrorCode property should be set to 26 if the file is either Encrypted or not a database file” (http://comments.gmane.org/gmane.comp.lang.realbasic.user/221815)

But this doesn’t seem to be true either.

How are you handling encrypted sqlite database files?

Is use this code :

[code] mDb = New SQLiteDatabase

Dim dbFile As FolderItem = SpecialFolder.Preferences.Child(“example.sqlite”)

If Not dbFile.Exists Then
CreatemyDB
End If

mDB.DatabaseFile = dbFile
mDB.EncryptionKey =“mykey+to+crypt”

If mDB.Connect Then
mDB.Decrypt
mIsConnected = True
MsgBox(“SQLite database connected.”)
Else
mDB.EncryptionKey =""
If mDB.Connect Then
mDB.Decrypt
mIsConnected = True
MsgBox (“SQLite database connected and not crypted.”)
else
mIsConnected = False
MsgBox ("Error connecting SQLite database: " + mDb.ErrorMessage)
mDB.Encrypt (“mykey+to+crypt”)
mDB.Close
End If
End If

If Not IsConnected Then
MsgBox(“Create the database first, the table and add the data.”)
End If

[/code]
maybe can help you… :slight_smile:

this is what I use…

    Cypher_Key=""
    db.EncryptionKey=Cypher_Key
    If DB.Connect=False Then
      If db.ErrorCode=21 Or db.errorcode=26 Then' File is encrypted or is not a database
        //
        Do
          Cypher_Key=Get_db_Password(False) ' ask user for key
          If Cypher_Key="" Then  'user gave up
            Quit
            Return
          End If
          db.EncryptionKey=Cypher_Key
          If db.Connect Then Exit Do
        Loop
        //
      Else
        msgbox "connection error"
        Quit
      End If
    End If

Thanks for the examples!

@Valdemar De SOUSA - there’s no reason to Decrypt the database. I would leave it encrypted on disk. Your app will be able to read it, because you have supplied the encryption key, but nobody else will be able to read it. If you decrypt it during the execution of your app, then it is available to anyone for the duration of your program. And if your app crashes, you would leave it unencrypted on disk.

Right Tim, in my case I want that the database stay decrypted until the end of the App. But in other case, to let the database encrypt just remove the ‘mDB.Decrypt’ lines.