Search code examples
androidsqliteandroid-studiodb-browser-sqlite

database data not fully shown in mobile when run


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!


Solution

  • 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.