I have an Android application using data from an external SQLite database in one activity. First, I made a connection with this db, and getting data was instant. Then, before publishing, I decided to use SQLCipher to encrypt the database with a password, but after this SQLCipher implementation, this particular Activity
became extremely slow (takes much more time to start, and displays each data item on button click).
Is that normal?
How to solve this problem?
Here is the way I did the implementation:
1- Add dependencies
// SQLCipher
implementation("net.zetetic:sqlcipher-android:4.5.6@aar")
implementation("androidx.sqlite:sqlite-ktx:2.4.0")
2- Make AssetDatabaseOpenHelper.kt
calss
class AssetDatabaseOpenHelper(private val context: Context) {
companion object {
const val DB_NAME = "dbName.db"
const val DATABASE_PASSWORD = "passord was here"
}
init {
System.loadLibrary("sqlcipher") // This loads the SQLCipher native libs
}
fun openDatabase(): SQLiteDatabase {
val dbFile = context.getDatabasePath(DB_NAME)
if (!dbFile.exists()) {
copyDatabase()
}
val db = SQLiteDatabase.openOrCreateDatabase(
context.getDatabasePath(DB_NAME),
DATABASE_PASSWORD,
null, null
)
db.rawExecSQL("PRAGMA key = '$DATABASE_PASSWORD'")
return db
}
fun copyDatabase() {
context.assets.open(DB_NAME).use { inputStream ->
context.getDatabasePath(DB_NAME).outputStream().use { outputStream ->
inputStream.copyTo(outputStream)
}
}
}
// get all data from table
fun getTableData(tableName: String): Cursor {
val db = openDatabase() // open database
return db.rawQuery("SELECT * FROM $tableName", null)
}
}
Finally, each time I start this Activity
I get the initial data by calling getTableData()
function at onCreate()
, and when button clicked I start a new Avtivity
and get other table data with same function.
That’s it.
Is the problem is the way of my implementation, or that is normal?