unable to open encrypted database using amitshekhar - android

Hello I am using this library since long but without sqlcipher now I have converted my database into sqlcipher and followed all steps which are defined here https://github.com/amitshekhariitbhu/Android-Debug-Database , after application launch when ever I open IP for its showing shared prefences data correctly but when I click on my sqlcipher database it shows nothing and getting log also
In Build.gradle
debug {
resValue("string", "DB_PASSWORD_Obee_MVP", *****")
libraries implemented
debugImplementation 'com.amitshekhar.android:debug-db-encrypt:1.0.6'
When i click on my database I am getting this in logs too
2021-04-22 13:24:05.712 7274-7372/com.xxx.xxxE/ClientServer: Exception.
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:89)
at net.sqlcipher.database.SQLiteCompiledSql.(SQLiteCompiledSql.java:62)
at net.sqlcipher.database.SQLiteProgram.(SQLiteProgram.java:91)
at net.sqlcipher.database.SQLiteQuery.(SQLiteQuery.java:48)
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2016)
at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1902)
at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2669)
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2599)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1247)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1214)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1186)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1135)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1336)
at com.amitshekhar.debug.encrypt.sqlite.DebugDBEncryptFactory.create(DebugDBEncryptFactory.java:15)
at com.amitshekhar.server.RequestHandler.openDatabase(RequestHandler.java:187)
at com.amitshekhar.server.RequestHandler.getTableListResponse(RequestHandler.java:306)
at com.amitshekhar.server.RequestHandler.handle(RequestHandler.java:113)
at com.amitshekhar.server.ClientServer.run(ClientServer.java:77)
at java.lang.Thread.run(Thread.java:923)

Related

android studio SQL lite database error (android.database.sqlite.SQLiteException)

I copy/paste the already created .db file to my android studio project, but it is crashing my app. That database file is working fine on another android studio project but it is giving exceptions and causing the app to crash on my current project. Following is the logcat
2022-05-26 19:42:18.147 21758-21758/com.learning.kidslearningzone E/TAG: setAppAdId:BeforeChange::::: ca-app-pub-3940256099942544~3347511713
2022-05-26 19:42:18.147 21758-21758/com.learning.kidslearningzone E/TAG: setAppAdId:AfterChange:::: ca-app-pub-3940256099942544~3347511713
2022-05-26 19:42:30.180 21758-21758/com.learning.kidslearningzone E/SQLiteLog: (1) no such table: kids in "SELECT * FROM kids WHERE id=2"
2022-05-26 19:42:30.183 21758-21758/com.learning.kidslearningzone E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.learning.kidslearningzone, PID: 21758
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learning.kidslearningzone/com.learning.kidslearningzone.Courses.ListVideoActivity}: android.database.sqlite.SQLiteException: no such table: kids (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM kids WHERE id=2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.database.sqlite.SQLiteException: no such table: kids (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM kids WHERE id=2
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1463)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:901)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2063)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:2002)
at com.learning.kidslearningzone.Databases.DatabaseHelper.getVideoDetails(DatabaseHelper.kt:20)
at com.learning.kidslearningzone.Courses.ListVideoActivity.setRvVideoListAdapter(ListVideoActivity.java:60)
at com.learning.kidslearningzone.Courses.ListVideoActivity.initDefine(ListVideoActivity.java:49)
at com.learning.kidslearningzone.Courses.ListVideoActivity.onCreate(ListVideoActivity.java:39)
at android.app.Activity.performCreate(Activity.java:8207)
at android.app.Activity.performCreate(Activity.java:8191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:246) 
at android.app.ActivityThread.main(ActivityThread.java:8633) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 
plz someone tell me what is the problem or what i have done wrong. Thanks
Every project has its own database may it be Room or Sqlite once you build your project the database will be created locally, so it is unique to the project and your need to add elements manually. What you did is you brought that file to another project and the query which you wrote is very SUS cause your present project database do not have any id:2 entry so it is failing.
You cannot just cut and paste the database into the project. For such a pre-existing database you would typically copy and paste the database into the assets folder and then use an adapted custom class that extends SQLiteOpenHelper (from your stack trace Databases.DatabaseHelper).
This adapted custom class MUST, in addition to the usual implementation have code that will:-
check to see if the database already exists.
if the database file does not exists copy the database file from the assets folder to the ultimate location (typically data/data/<the_package_name>/databases/<the_database_file_name)
Should the database then not exist (if no exception has stopped the app) or if the database is corrupt then the database will be created WITHOUT anything but the sqlite_master table and the android_metadata table, so effectively , from the user/developer viewpoint, an empty database and hence a table not found.
I would suspect that this is what is happening.
However, another scenario where this can happen if exceptions are caught/trapped and allow processing to continue. Is when the SQLiteDatabase's getWritableDatabase or getReadableDatabase are used for later Android versions when WAL (Write-Ahead Logging is the default). In short this creates a -wal file, which is NOT owned by the original database file created by the methods as that database file has been overwritten by the one copied from the asset. The too friendly openOrCreate method used then sees the corrupt database and gives you a new database devoid of any tables.
It is suggested that one of the above is the issue that you face. Although a bit over the top this may be of use.

Android app crashes after generating signed apk

UPDATE:
When I delete app storage, it starts to work. I don´t understand...
When I generate a signed APK for my project and I install it to my phone, app crashes.
When I debug the app, it works correctly.
I use Android Studio 3 and I disabled instant run.
Using logcat, I get the following exception:
2019-01-15 19:19:30.594 7317-7317/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.agomezgz.bng.programa, PID: 7317
java.lang.RuntimeException: Unable to start activity ComponentInfo{me.agomezgz.bng.programa/me.agomezgz.bng.programa.SplashActivity}: android.database.sqlite.SQLiteException: no such table: comentario (code 1 SQLITE_ERROR):  
it complains about no such table: comentario ...which means that table comentario had not been previously created - and that the stack-trace is just a follow-up error. see the build output for the ProGuard warnings (or even add them to the question); there might something obfuscated, which should not have been obfuscated. the code of the database class is completely irrelevant (simply because it works, while not being obfuscated); only the build-log matters. adding -verbose into the ProGuard configuration might help to obtain some more details.
UPDATE: When I delete app storage, it starts to work. I don´t understand...
You had an earlier version of the database file around that did not have that table. Clearing app storage removed the database file and forced sqlite helper onCreate() to run again.
See When is SQLiteOpenHelper onCreate() / onUpgrade() run?
Why it worked in debug is that the debug package is another application id and has a separate private directory where the database files are stored. That database file did not have this problem.
change your database onCreate method to this :
db.execSQL("CREATE TABLE IF NOT EXISTS comentario(_id integer,
nome text not null,
correo text not null,
texto text not null,
idLoc integer not null, primary key (_id));");

[Android]: Firebase database causes SQLiteDatabaseLockedException

We used Firebase Database to synchronize data and we found an exception in crash report. Here is brief log:
Caused by com.google.firebase.database.DatabaseException:
Failed to gain exclusive lock to Firebase Database's offline persistence.
...
Caused by android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): ,
while compiling: PRAGMA journal_mode
More detail log is here. Here is our Firebase Database init code
// run in Application onCreate method
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Mostly, HTC mobiles with API level 19 are impact. We try to reproduce this
bug, but fail.

Unable to update database in android using SQLiteAssetHelper

I'm using SQLiteAssetHelper. I tried to upgrade the database by adding few insertions into the testdb.db file then according to instructions on
https://github.com/jgilfelt/android-sqlite-asset-helper
I renamed this new file to testdb.db_upgrade_1-2.sql and added to the asset folder in the android app. I changed the database version to 2 in the Databasehandler, but still there is an error as shown in the stacktrace which is as follows
I/SQLiteAssetHelper: successfully opened database testdb.db
W/SQLiteAssetHelper: Upgrading database testdb.db from version 1 to 2...
W/SQLiteAssetHelper: processing upgrade: databases/testdb.db_upgrade_1-2.sql
E/SQLiteLog: (1) near "SQLite": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
After this the app crashes.
My asset folder contains both /databases/testdb.db
and /databases/testdb.db_upgrade_1-2.sql
I'm using sqlite3 on my computer to generate the database file
I renamed this new file to testdb.db_upgrade_1-2.sql
That is incorrect. testdb.db_upgrade_1-2.sql is supposed to be a text file containing SQL statements to apply to your schema v1 database to convert it into a schema v2 database.
Quoting the documentation, with emphasis added:
Update the initial SQLite database in the project's assets/databases directory with the changes and create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version and place it in the same folder.

SQLite-db corrupt only on Android 2.2

I'm using this method to copy a pre-existing sqlite-database from my assets-directory to the app's /databases/-directory on the first app launch. This works fine on emulators with different SDK-levels and on my 4.4 devices, but fails on a 2.2.2 device with the following logcat message:
sqlite returned: error code = 11, msg = database corruption found by source line 40107
and further on SELECT locale FROM android_metadata failed
There are other SO-questions with this error, but my database definitely contains an android_metadata-table with a locale column and an en_US entry (which is why the app works on many other emulators and higher-SDK devices) and isn't large (32KB), which I read can be an issue.
Update: I switched to using the SQLiteAssetHelper-library, but am still getting the same error:
copying database from assets...
database copy complete
sqlite returned: error code = 11, msg = database corruption found by source line 40107
sqlite returned: error code = 11, msg = database disk image is malformed
CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
It seems the SQLiteOpenHelper (not the AssetHelper) is trying to create the android_metadata table (even though it's already present) and fails because of this?
Update 2: The full stacktrace can be found here
and add a new android_metadata-table with a locale-column and an en_us-row
Now that you are using SQLiteAssetHelper, and given the error message (CREATE TABLE android_metadata failed), remove your manual android_metadata table from your packaged copy of the database. This will be created for you as part of unpacking the database via SQLiteAssetHelper.

Categories

Resources