I am using sqlite to save some string values in the database, my app works perfectly in the emulator. But when I tried it on the Samsung Galaxy Tab, the log cat says that there are not such tables... before mounting the apk files to the device I have tried opening it in the archive mode and found the database and the tables secure in my "assets" folder.
Can any one tell me whats going wrong with me ?
thanks in advance...
Happy coding
Databases don't belong in the assets-folder! You should use a SQLiteOpenHelper-class to create and update your Database when it's needed.
Tutorial (or simply Google for it).
I have experience with the same thing you are experiencing now. This is a classic in device manufacturer fragmentation. It has happened to me before when trying to run an application on a Samsung Nexus S phone that had a method to copy a already existing database to the application from assets on install (i am guessing that's what you are doing at the moment). so long story short, after quite some testing the best and only solution i could find was to actually do the manual work and create the database with the help of the SQLiteHelper methods and table creating queries and recreate the same scheme. this method works 100% so far on every phone i have tried it so that's the safest thing you can do.
Hope this helps.
The db file should be available in "/data/data/YOUR_PACKAGE/databases/" folder in your device.
If this is the first time you are creating the db then you should copy the db file from your assets folder into the above location (the first time when you access the database).
Related
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).
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 = . 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
. But, when I run it on an emulator, the updated data are shown at
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.
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
As such, does anyone know what could cause this issue and how I can fix it? Any help is greatly appreciated!
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.
I have an Android app where I use a SQLIte DataBase. I am using the app and the DB is already big. Now I want to give this app with its DB to my coworkers. Where and How to put the DB for release? I have the DB in my phone but I need it in assets folder. I was trying but it doesn't work. I tried to copy the DB directly however I read that Android compress files in that folder. Please, any solution, thank you in advance.
http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Visit this link. It contains the easiest and well described answer for your question.
You can use emulator Like GenyMotion and any other emulator. Run your app on emulator then just go to Android Studio->Tools->Android Device Monitor Then select the emulator and in the file Explorer you can find your db file . and then export from the device and export to your desktop. here you can give it to any one.
You can use your own SQLite database by adding it to assets folder. The best way is to use Android SQLiteAssetHelper. Better than reinventing the wheel.
Here the excerpts from its readme:
An Android helper class to manage database creation and version
management using an application's raw asset files.
This class provides developers with a simple way to ship their Android
app with an existing SQLite database (which may be pre-populated with
data) and to manage its initial creation and any upgrades required
with subsequent version releases.
It is implemented as an extension to SQLiteOpenHelper, providing an
efficient way for ContentProvider implementations to defer opening and
upgrading the database until first use.
Rather than implementing the onCreate() and onUpgrade() methods to
execute a bunch of SQL statements, developers simply include
appropriately named file assets in their project's assets directory.
These will include the initial SQLite database file for creation and
optionally any SQL upgrade scripts.
I had a problem with my pc and I had to format the c drive without being able to access it, good things is that i only lost my app files, bad news is that I lost my app files hahah (not really laughing at all, more like a crying laugh). I used to test my app in my phone so i have it installed there, i have half of the progress I've made backed up (too bad i didn't change the app's folder, I am gonna do it from now on) but i would reaply like to recover it, so... Is there any way that can be done? If not well... learned the lesson the hard way I guess
You may be able to retrieve the java classes and resources from the apk. First, you can try pulling the apk from your Android device:
How do I get an apk file from an Android device?
Second, try retrieving java files from the apk:
how to extract code of apk file
It is called reverse engineering.
Here's a tool that might help you: https://ibotpeaches.github.io/Apktool/
Hope you will recover your code ;)
Im REALLY struggling to get a pre baked db4o database to work inside my android app.
If i include it in the /res/raw folder then read it, it doesn't work.
If i copy it to the app_data or sdcard then try and read it, it will act as if im opening a new database and provide me with 0 entries on queries.
I was previously just creating all my entries when the app was first opened but as the db grew, so did the creation time to unsustainable levels.
I would really, really appreciated any help you can give me. It would be perfect if i can include my pre-populated db4o file in the app.
Additional Info:
I have double checked the db4o file with ome and theres no issue with the db creation on my local machine.
Thanks in advance.
If any one falls into the same trap as me here's the answer:
On android the class is injected into the db4o db with the package name prefixed to the class name by defualt. So accessing from another application will look be looking for +.
Some of my users are reporting that they cannot correctly open sqlite databases created in Android using sqlite jdbc or other tools like sqlite browser (http://sourceforge.net/projects/sqlitebrowser/).
Here is an example database with the problem:
http://pocket-for-android.1047292.n5.nabble.com/Desktop-Version-Screenshots-Work-In-Progress-tp4383220p4456017.html
This sqlite db was created on a HTC Sensation, running Android 2.3.3 and Sense 3.0.
The exact problem is that if you look at the file in a text editor you can see a bunch of tables being created but if you try and run "select * from groups;" you will get an SQLException: file is encrypted or is not a database.
I have tried using "select sqlite_version;" to see if it is a versioning issue - the sqlite browser says 3.6.18.but different tools give me different version numbers... not sure what is going on with that.
I should also point out that my app copies the db directly from the device (/data/data/com.citc.wallet/databases/wallet.db) onto Dropbox.
Any help would be really appreciated.
The solution was that the zentus project is not working with the latest versions of SQLite.
Fortunately someone is keeping a fork maintained here: http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
All I had to do was replace the jar with the xerial one and it all worked. Xerial sqlite jdbc is in my opinion the best way of connecting to SQLite in Java (I tried several of the libs).
I am also getting the same problem.
But once I have changed the database name it resolved.
So, just try it by creating new database