android development, db file does not get updated - android

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

Related

Can't find where my database file is being copied to my phone/emulator when I install to it (possibly general android/assets folder Q)?

So it seems that when I run my app on my phone / a phone emulator that the database file is not ending up in the correct directory over there. It seems that the app is looking for it in data/data/com.mobiletextadventure/databases. It doesn't find it there and then creates a new empty database in that directory as far as I can tell and uses that.
If I manually copy the database file to data/data/com.mobiletextadventure/databases after I have already installed the app and then run it, it picks up the database and doesn't create a fresh empty one. This is what I want obviously, but I need it to do this on initial install rather than having to manually copy it across after installing.
In my project in android studio the database is in android/assets/ folder. How do I find out where exactly this database is being copied to my phone / emulator? It's clearly not going into data/data/com.mobiletextadventure/databases, but if I can get it to install into there then my app will work fine without any manual intervention.
Another way of asking this question might be... when an app is installed to phone/emulator, what exactly happens to the files in /android/assets? Where are they copied to on the phone/emulator and why?
How do I find out where exactly this database is being copied to my phone / emulator?
It is not getting copied anywhere, unless you do the copying.
when an app is installed to phone/emulator, what exactly happens to the files in /android/assets?
Absolutely nothing. They are part of the APK. You can obtain an InputStream on an asset via AssetManager (see getAssets() on Context).
If you are seeking to package a database as an asset and use it, you can see how SQLiteAssetHelper does that.
In the end I was able to achieve it by following this post:
https://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I initially got an uknown error code 14 after implementing it but all I had to do was replace the checkDataBase() code with the code below and it worked perfectly:
File dbFile = myContext.getDatabasePath(DB_NAME);
return dbFile.exists();

Browse Android SQLite with Datagrip

I'm new to Android and I have been trying to connect my SQLite db with DataGrip (because I already use it and I feel confortable with it).
I wanted to try this way but dropdown didn't show any Android SQLite option.
I also tried this way, but I couldn't find the .db file in my Android project.
Any help will be nice, thanks!
I found a way that is maybe a bit tricky and not helpful at all, but at least I can browse into the db:
Enter the Device File Explorer on Android Studio
Copy the database you need into your local folder (path is data/data//database.db)
Then enter datagrip and create a new data source (SQLite), using this file.
It's a really anoying way, because each time you make any changes, you need to re-copy the file, and sync your datasource. But anyway, it works.

Why database gets deleted when an apk has been reinstalled?

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.

Android database on phone

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.

How to update a database used as a source of data by an Android app

I have an app which uses a large amount of data which has been compiled outside the app (on my main PC). The app is for my personal use so there are no complications with having to distribute data updates to other users. However, I am currently following a very convoluted and time-consuming procedure each time I want to update the data, and I wonder if anyone can suggest any ways to streamline it.
The procedure I follow whenever I want to add new data is as follows:
I enter the new data into a csv file which I maintain as the source of the relevant table in the database
I use SQLite Database Browser to import the data into an existing SQLite database. (This program does not seem to have the ability to append imported data into an existing table, so whenever a table needs updating I have to delete the existing table, then import data from the csv file into a new table, then manually edit the data types for all the fields in the table.)
I drag the icon for the database file onto the 'assets' folder of my project in Eclipse.
I export the project from Eclipse as an apk file.
I copy the apk file to my phone
(using Astro File Manager) I uninstall the old version of the app and install the new apk.
when the app is run, code based on the example set out here copies the data from the 'assets' folder into the app's data folder; this means that each byte of data takes up two bytes in the phone's internal memory; at present this is not a problem, but could be as the volume of data grows; I wonder if there is a more memory-efficient method?
I should be very grateful for any guidance.
I have found that I can have the SQLite database on the SD card, which simplifies things for me considerably (and reduces the amount of internal memory occupied by my app.)
The first clue was this blog which told me that it is possible (contrary to the impression I had gained from the Android documentation and from my earlier googling) for an app to refer to a database on the SD card.
I then found this page here on Stack Overflow which points out how simple the code can be.
With the help of the advice on these pages, I now have much simpler DBHelper code in my app where the openDataBase() method simply reads as follows:
public void openDataBase() throws SQLException{
File dbfile = new File(DB_PATH + DB_NAME);
myDataBase = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
}
There is no longer any need to include the database in my project's "assets", nor therefore to include it in the apk file. And the great advantage is that the database can be updated without having to go anywhere near Eclipse, let alone reinstalling the app.
I don't really know about the database but I do know that if in eclipse you run the app on your phone it will automatically install it. So instead of running it in the emulator when you press the play button, run it on your phone and many of those steps will be eliminated.
To Wipe Database Clean on startup on the emulator:
Click the down arrow next to the play button in eclipse
Select Run Configurations
Click the Target Tab
Check the "Wipe User Data" check box

Categories

Resources