1

This will be a long post but please do read until the end and help out. Thank you!

In continuation of my previous post, [Android Studio - Database file loaded in the wrong encoding: 'UTF-8' my app was working fine when I run it both on my phone & an emulator despite the encoding error.

However, I am facing new issues now and I would like to just clarify why.

Just a head's up, I am using DB Browser for SQLite & Android Studio (3.2.1). My phone is Samsung S7 Edge+ and the emulator I have used is Pixel 2 XL API 28 (Android 9, API 28).

  1. The first issue is that I have added new data into my database but it is not reflected when I run it on my phone. See attached for reference to new data added "database - knowledge.db" database = Knowledge.db. I have also ensured that the data is updated by clicking the "Write Changes" tab. Afterward, to import the database to Android Studio, I have to create a database asset folder and stored my Knowledge.db file inside it. I have done so. However, when I run my app on my phone, it does not show the updated data when I scroll down, see the attached actual phone actual phone. But, when I run it on an emulator, the updated data are shown at phone emulator

phone emulator. The new data are those titled, "IIDS" "FIDS" and "GMID". Notice the two phones screenshots, the actual phone screenshots stopped at "Passenger Terminal" and upon scrolling down further no new data are shown, but on the phone emulator, new data are shown.

Initially, I thought it could be the sizing issue so I minimized the text sizes accordingly but the issue persisted. I can't think of other possible causes.

  1. Secondly, I know I have updated the database at DB Browser by clicking the "Write Changes" because when I open it again, the new data and naming changed. But when I import it to Android Studio, it is not fully updated. See attached and the circled for reference. differences differences

As such, does anyone know what could cause this issue and how I can fix it? Any help is greatly appreciated!

Rutvik Bhatt
  • 3,185
  • 1
  • 16
  • 28
  • Have you deleted the App's data on the phone? If not try doing so and the rerunning the App. – MikeT Oct 26 '18 at 04:21
  • @MikeT Hi, I have done this and now I have this error, "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference". Could this be due to the DB_VER=1 I have declared previously? –  Oct 26 '18 at 04:25
  • If you changed any table structure then you need to increase your db version – Al-Amin Oct 26 '18 at 04:40
  • Either you need to uninstall the previous installed app and re-install. – Al-Amin Oct 26 '18 at 04:49
  • It would appear to be version related. Try checking the version in DB Browser using **`PRAGMA user_version`** and perhaps setting it e.g. using `PRAGMA user_version=1` (where 1 would be the value of DB_VER in the App). At a guess it might be that DB_VER is lower than what is in the DB, (although the crash should indicate that there is no `onDowngrade` method) (overriding onDownGrade to do nothing could be a get-around). It might just be a matter of changing DB_VER if the value in the database is greater that the value in the App. Uninstalling the App and re-installing the App may work. – MikeT Oct 26 '18 at 04:56
  • @MikeT Hi! I checked the Pragma user_version at DB and initially it was 0! I then upgraded it to 1 and likewise amended the DB_VERSION at my codes to 1 too! Now the updated database are reflected at both phones! Thank you so much for your help! :) –  Oct 26 '18 at 05:57
  • @Al-Amin Hey! Noted with many thanks ya! While I finally understand that every changes made to the database must increase the version, but I don't understand whether it is applicable to the pragma user_version (in DB Browser) or is it the Android Studio's DB_VERSION =1? Because when I amended the DB_VERSION=2, there was an error saying I could not upgrade a read-only database from 1 to 2. As such in the future, where do I make changes of version at? –  Oct 26 '18 at 06:00

1 Answers1

1

The initial issue was due to the database already existing and thus that the copy from the assets folder is/was not done.

Delete the database (deleting the App's data or uninstalling the App) would result in the database then being copied from the assets folder.

However, you then encountered an issue with the version number. I believe that this was because the App had been changed to use database version 2 (actually from SQLite's point of view the user_version). Thus as the version isn't 1 (I suspect if it's 1 no version check is made, hence why not even having a version (as from the comments was the case)) then an attempt was made to check the DB's version resulting in a null pointer exception as there was no user_version set (perhaps a bug).

Basically you do not want to change the DB version from 1 if you are (when developing the App) re-introducing a changed database that is copied from the assets folder. Alternately you need to set the appropriate version using PRAGMA user_version=? (where ? is the version number) in the database, using whatever tool you use, before copying the databse into the assets folder.

The only reason why you would increase the database version (android wise) is when you are wanting the onUpgrade method to run.

  • Note assumptions have been made re checking the version number.

  • If you have released an App, then it could be far more complex to roll out a changed database as an asset.

MikeT
  • 51,415
  • 16
  • 49
  • 68