Android Question Attach database to encrypted (SQLCipher) database?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
As the title: is it possible to attach a (encrypted or non-encrypted) database to a database, encrypted with SQLCipher 1.5?
Whatever way I do this (tried lots) I get this error:

net.sqlcipher.database.SQLiteException: file is not a database: ATTACH DATABASE '/storage/3637-6230/Android/data/b4a.testing/files/SQL.db' AS 'DBA'

B4X:
strAttachResult = AttachDB(Starter.strAppDir & "/" & strDBNew , "DBA")

Sub AttachDB(strDBFolder As String, strDBName As String) As String
 
 Dim strSQL As String
 
 strSQL = "ATTACH DATABASE '" & strDBFolder & "' AS '" & strDBName & "'"
 
 Try
  cConnection.SQL1.ExecNonQuery(strSQL)
 Catch
  ShowError("Error in AttachDB", "")
  Return "Error"
 End Try
 
  Return ""
   
End Sub


RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Got this working nicely now.

This is how the AttachDB Sub should be:

B4X:
Sub AttachDB(strFullDBPath As String, strDBName As String, strPassword As String) As String
 Dim strSQL As String
 If strPassword.Length = 0 Then
  strSQL = "ATTACH DATABASE '" & strFullDBPath & "' AS '" & strDBName & "'"
 Else
  strSQL = "ATTACH DATABASE '" & strFullDBPath & "' AS '" & strDBName & "' KEY '" & strPassword & "'"
 End If
 Try
  cConnection.SQL1.ExecNonQuery(strSQL)
 Catch
  ShowError("Error in AttachDB", "")
  Return "Error"
 End Try
  Return ""
End Sub

Thanks for getting me on the right track.


RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User

This works attaching an encrypted DB to an encrypted DB.
Not figured out yet how to attach an unencrypted DB to an encrypted DB.
Off now, will check this later.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This works attaching an encrypted DB to an encrypted DB.
Not figured out yet how to attach an unencrypted DB to an encrypted DB.
Off now, will check this later.

RBS

This works fine:

B4X:
Sub AttachDB(strFullDBPath As String, strDBName As String, strPassword As String) As String
 Dim strSQL As String
 If strPassword.Length = 0 Then
  strSQL = "ATTACH DATABASE '" & strFullDBPath & "' AS '" & strDBName & "'"
 Else
  strSQL = "ATTACH DATABASE '" & strFullDBPath & "' AS '" & strDBName & "' KEY '" & strPassword.Trim & "'"
 End If
 Try
  cConnection.SQL1.ExecNonQuery(strSQL)
 Catch
  ShowError("Error in AttachDB", "")
  Return "Error"
 End Try
  Return ""
 End Sub

So we can do with a key or without.
It looks attaching an unencrypted DB to an encrypted one you still need the key, so supply the password as " ",
so it will do with a key but do the key as "".

RBS
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…