Android Question Attach database to encrypted (SQLCipher) database?

Discussion in 'Android Questions' started by RB Smissaert, Mar 17, 2019.

  1. RB Smissaert

    RB Smissaert Well-Known Member Licensed 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'

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

    Sub AttachDB(strDBFolder As String, strDBName As StringAs 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

  4. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Got this working nicely now.

    This is how the AttachDB Sub should be:

    Code:
    Sub AttachDB(strFullDBPath As String, strDBName As String, strPassword As StringAs 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
     
  5. RB Smissaert

    RB Smissaert Well-Known Member Licensed 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
     
  6. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    This works fine:

    Code:
    Sub AttachDB(strFullDBPath As String, strDBName As String, strPassword As StringAs 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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice