I have a problem regarding android database.I have an app which have it's database in assets folder.This app is already installed on the phone and working fine.But now i created another database file(just increase the number of records in the previous database and everything else is same). And put it again in assets folder and replace it with existing database file, and recompile the project. Now in emulator the new database is loading and showing the updated content in database. but when i installed the app again in phone my previous database is showing up and new database is not loading.
But if i uninstall the app from the phone and reinstall it then new database is showing up.
PROBLEM: I just want that if i replace the preexisting app on phone the new database should be loaded, not after uninstalling and then reinstalling the app.
Any suggestion or help?
In Android you must use the onUpgrade function to update your database from a previous version. This is because Android will only create your database once and from then on out it will just use that one. So you must assign a version code to your new database that is different from the old one and use the onUpgrade to actually perform the change to the new database. All devices that are installing from scratch do not have a current database so they are getting the new one created for them.
I bet if you clear the data for your app in Settings and relaunch it, it should load the new database. The reason for this has something to do with SQLiteOpenHelper copying the database from the assets folder in the APK to the application's data directory on the phone if it doesn't already exist. Otherwise it will load the one on the phone. Unless you specifically access the database from the assets folder each time, it will use the cached version.
Related
I have created a database and put it under "databases" directory in my project(using eclipse). When I lunch an application for the second time after changing the database file, old database file from phone does not get deleted a new fresh copy is not installed to the phone. I was wondering if anyone experienced something like this?
Any possible solutions?
Thanks,
I have experienced that when just running the app from Eclipse after doing some file modifications, the old files that were on the phone stay. This includes Shared Preferences, db files etc...
Solution: To be sure that new files will be installed delete your app from your phone manually if this occurs, and then click Run in Eclipse.
Also another bug that happens rarely to me is that i change the code and when running the app the old code executes? Eclipse gets into buggy state sometimes and needs to be restarted if you experience some weird behavior. (this used to happen to me with old ADT before update)
I have just created A Sqlite database in my app firstly i am having the same problem then i created my new database name with the old name and replaced my old one .so i am agree with #ved it creates new db file and also keep old db file.
Android does not delete db file automatically from application.When you change the name of your previous db filename to new filename it creates new db file and also keep old db file.
I solved my problem by using a helper class for database connection and the problem was gone.
You can find it here:
https://github.com/jgilfelt/android-sqlite-asset-helper
I have an android application that uses a database created by SQLite Database Browser as shown in this blog: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
First, I created a database with two tables with one of those tables with one entry, just to check if I was getting the database creation correctly. This worked.
After that, I populated one of the tables with 10 more entries, saved, then copied the database to the assets folder (overwriting the previous one), ran the application, and then queried the 2nd - 10th entries. The application was force closing giving me the CursorIndexOutOfBounds error.
From this, I inferred that the application was somehow unable to use the new database I created and was still using the old one (that was overwritten).
Is there a way to get around and/or fix this? The application seems to be using the first version of the database it receives, even if that database is overwritten by a new one.
It sounds like your first DB, the one with only a single entry, still exists in your app's database folder under /data/data/your-package-name (on your android device). That tutorial's code will only copy the database from the assets folder if the database does not exist on the device (look at the checkDataBase() method in the tutorial). So, delete the database from the /data/data/your-package-name (either with code or with root access), make sure your updated DB file is in the assets folder, and then run it again. That will ensure the DB from the assets folder is copied onto the device.
Also, make sure the DB_PATH is equal to context.getApplicationInfo().dataDir + "/databases/" instead of "/data/data/YOUR_PACKAGE/databases/".
My proble is I created a device in sqlite database browser and its work fine in emulator.
but when a try my application to my device it cause force close.
and when I remove the database and create a new one using eclipse it works fine even in my device. But inserting a lot of data in database 1 by 1 realy freakin me out so anyone know a sqlite database browser that also work for device??!
check out this tutorial for using database file in application.
Remember one thing that it only copy you DB file in application that's it.
On real device you don't have permissions to sqlite database. That's why you won't be able to browse database like you can on a emulator.
See this for a work around.
Tool to see android database, tables and data
If you want to create a pre-loaded DB outside of your app and use it in your app later, you will have to put it in your assets directory and then copy it to your apps storage space the first time your app is started.
This is due to the fact that as a security measure, on real (unrooted) devices nothing but your own app is allowed access to that apps databases directory (and certain other ones as well).
There is a good tutorial for creating/copying your own DB outside of the app and copying it to the apps databases directory here.
I have a problem related to the app that I developed which runs on honeycomb. When I reinstall the apk its database gets deleted. There did not used to be this almost 1 week ago. Why this is happening now? What can cause db to be deleted and how to prevent it?
I think, If your database stored in application's internal storage /data/data/<package_name>/databases/ then when your application un-installed from device, all directories with your application package are removed from the device this cause your database removed.
To prevent put your database copy in application's /asset directory so whenever your application first time runt it copy the database from asset to internal storage path. And you can access it whenever application re-installed, also you can put your database in /sdcard but user can also delete it..
EDIT: Using your own SQLite database in Android applications and How to ship an Android application with a database?
Thanks...
i guess it about checking existing database in your code. but i'm not sure exactly, it can occur from many causes. Just give an idea.
I am developing an android application with inbuilt SQLite database. I need to replace SQLite database file only every 3 months. while doing this I should not alter any part of code.
Also I don't want to do this as upgrading application where changing app version I can replace old application with newer version and new database.
For example if I give a button "Update Database" and after clicking this button only database file is replaced using internet and other parts of application remains intact.
simply do as you'd do with any file:
Download the database file from a web server to the devices temp directory (or cache or SDCard)
Move that file to your application's data directory
To do that automatically, you should use a service started by the AlarmManager.