Android Question SqlCipher - recreate current database structure to SQLCipher Db

GeoffT660

Active Member
Licensed User
Longtime User
Do I need any files from Zetetic Sqlcipher? The structure of my SqlLite db is currently edited with SqlLite Maestro, what do I need to do to recreate my databases for SqlCipher?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
I still am unable to figure this out and have tried many variations to try and create a encrypted database and create the tables in the new encrypted database. I'm trying:
B4X:
DB.Initialize(Dir, "MyDb.db", True)
    If File.Exists(Dir, "MyDbE.db") Then
        File.Delete(Dir, "MyDbE.db")
    End If
    DB.ExecNonQuery("ATTACH DATABASE '" & File.Combine(Dir, "MyDbE.db") & "' AS MyDbE")
    SqlCipher1.Initialize(Dir, "MyDbE.db",True,MyPassword,"")
    txt="CREATE TABLE IF NOT EXISTS MyDbE.TableData AS SELECT * FROM TableData"
    DB.ExecNonQuery(txt)

Where MyDb is the unencrypted database and MyDbE is the encrypted one. When I go back and try to access the table after initializing I get "Unknown Database.
I'm just trying to:
1) create a encrypted SqlCipher database
2) Copy my tables from the unencrypted database to the encrypted database
3) Be able to read and write to the encrypted database or unencrypted database.
For how simple the tutorial is, I sure haven't been able to figure this out after many hours.

Please help.
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
I'm still not getting this. Can you please provide a small code snippet from my example above at how I can accomplish this or point me to the fundamentals that I must be missing.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Open the unencrypted database with SQL.
2. Create a new database with SQLCipher.
3. Create the tables in the new database.
4.
Now do something like:
B4X:
Dim rs As ResultSet = SQL.ExecuteQuery("SELECT * FROM table")
Do While rs.NextRow
 SQLCipher1.AddNonQueryToBatch("INSERT INTO table VALUES (?, ?)", Array(rs.GetString("column1"), rs.GetInt("column2"))
Loop
rs.Close
Dim SenderFilter As Object = SQLCipher1.ExecNonQueryBatch("SQLCipher1")
Wait For (SenderFilter) SQLCipher1_NonQueryComplete (Success As Boolean)
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
Thanks for this but how do you create the new database with SQLCipher? I don't even need to transfer any data, I just need to create the encrypted database and copy all my tables.
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
Thanks. I was doing that right so then how would I transfer the tables without data from my unencrypted database to my encrypted database?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See #6
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
Can you please give me some example code for transferring all tables from the unencrypted database to the encrypted one.
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
Although I would still love to figure out how to transfer tables between unencrypted database and encrypted one, I did find an alternative solution. Using DBBrowser for SqlLite and B4a bridge ftp, I was able to copy my populated tables to my computer, encrypt them by adding a password and copy them back to the device. Thanks for your help and if you can post a code example to transfer tables on the device mentioned above, that would be great.
 
Upvote 0
Top