-2

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

Saadia
  • 15
  • 3
  • `plz someone tell me what is the problem or what i have done wrong.` well, your error says `no such table: kids`, so from the info you've given, that's the problem. we have no way of checking what's in the db or checking the code which is generating it, so it's impossible for us to help here, are you sure you actually have this table ? – a_local_nobody May 26 '22 at 15:13
  • @a_local_nobody well I don't know about this as I have downloaded a project from the internet which contains a db file that is working fine with its own project, and when I copy/paste that db file to my project it is giving this error. I don't know what to do. – Saadia May 26 '22 at 20:23
  • @a_local_nobody and Thank You so much for your kind answer while everyone else is giving me downvoting – Saadia May 26 '22 at 20:29
  • on SO you need to provide enough info for others to help, so people are downvoting because your question needs more info - you have given us what your problem is with no way of recreating it, so for future be sure to add as much detail as you can – a_local_nobody May 27 '22 at 06:15

2 Answers2

1

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.

0

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:-

  1. check to see if the database already exists.
  2. 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.

MikeT
  • 51,415
  • 16
  • 49
  • 68