When i modify the database and upgrade the existing app in my Phone, the DB is not getting overwritten which makes my application crash.
How to tell phone to delete the DB and add fresh one during installation of APK file?
Did you try to increase the version number you pass to the the SQLiteOpenHelper constructor.
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Related
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 am not new to Android or Java but very new to Databases. I have been practicing with SQLite in Android and have now become completely stuck.
I want to completely remove the current database that I have been using in my app and create a new one with more columns, different types of columns, and etc...
I have tried "context.deleteDatabase()" which appears to delete the database but then after that I uninstall the app and re-install it with the myDatabaseHandler java class file having all the new columns and changed added to the file.
The code compiles and runs fine until I try to add info to the database, I receive an error cannot find columns, the error refers specifically to the columns that I added.
Why does it seem that I cannot start over completely. It seems like the structure or schema of the database won't change.
So how do I eliminate any evidence of the databases that I was practicing and messing around with, and just start completely new? After all, thats what practice is, you don't know what you are doing and you learn by making mistakes. So now I need to completely wipe away the mistakes, not upgrade just to make alterations.
Upgrading the database seems to provide an avenue for achieving close to what I want, but ultimately is way more involved and confusing for what I need when I just need to start over with a freshly created databases that has more columns.
The SQLiteOpenHelper class goes to great care to keep the old database to allow you to upgrade it in place.
If you're not interested in the old data, just change the file name. Then it is guaranteed that there is no old version. (You still have to call deleteDatabase() to get rid of the old file, but now that call cannot conflict with any accesses to the new file.)
Using the ADB tools from the SDK/platform-tools folder can help to remove all data (including the database schema)
./adb.exe -s shell pm clear <your app's package name>
will remove all the data associated with your app. Then you install the new version of the app, it will use the new database schema.
I have begun to programming in Android with Eclipse a little application who has a tiny sqlite database. As you know, sqlite is really easy to use. I have been using sqlite for many years, but now I have found somethig unusual and disappointing:
when I try to update manually my database (e.g. using SQLiteman or another SQLite GUI), changes are reflected when I make a simple SELECT * FROM... in SQLiteman. But when I execute my application and make another simple SELECT * FROM... the new row is absent! I doesn't appear!
-I have cleaned my solution and I have tried to update many times with "Import..." without any result.
-I suspect that it may be something related to an internal index not updated, but I'm not sure.
My code is minimal, as you will see:
String query = "Select _id, name FROM mytable";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
int totalRows=cursor.getCount(); //<---- This only shows the older total!
What's wrong with this?
Addendum:
I'm not using an emulator and my device is already rooted. But I can't see my database when I go to the DDMS in Eclipse (but this is another question).
I'm editing the database in my PC and then I import it to Eclipse. I have edited too directly in Eclipse, but without no luck.
Thank you
I guess you must be running the older version of your database. Just try to uninstall the old application from the emulator or device and install the new updated version.
I am sure it will reflect.
Whenever you change your database explicitly at that time to get it reflected into application you need to upgrade the version of your database otherwise it will not affect your database. Or you need to uninstall the old version of your application and install updated application that will reflect the SQLite.
Most likely, you are not doing a "COMMIT" on the new data, so it isn't persisting in the database in Android
Google documentation for SQLiteOpenHelper.onUpgrade is very general. I'm wondering how onUpgrade execution exactly works?
Does Android stores database version number inside database itself (I tried to find it using database tool aSQLiteManager, btw very good piece of software, but it seems to be hidden if exist)? Or does Android just compares the old and new version of app in the moment of installation and basing on this it definieds old and new version as parameters of onUpgrade.
Let's take an example. I have app verson 1 which can work with multiple databases. At the beginning I have only database A version 1. Next I upgrade app to version 2 and database A is also upgraded to version 2 (onUpgrade is executed). Next I get database B from my friend created by the same app but in version 1 (thus B has also version 1). I copy it to the appropriate dir and run my app. Will onUpgrade method be executed then (for database B version 1, no app reinstallation)?
If you want, you may want to use SQLiteDabase (the first parameter of onUpgrade), with the method getPath to know which databse will be updated.
When you will ask SQLiteOpenHelper.getWritableDatabase (or getReadableDatabase()), it will be updated if the version you set in constructor is greater than the current version of the database.
If you need different upgrade statements for different databases (which is generally the case), you may want to declare two classes extending SQLiteOpenHelper.
I've created an Android application which has a certain package name that I've been using personally for months now. I'm about to release it on the market, and I have to change the package name. This cannot be avoided.
My issue is that the application has an SQLite database attached to it that I want to keep, but I know if I change the package name, it'll install as a separate application and I'll have to restart my database, which would take a very long time.
Is there a good way to change a package name while maintaining the SQLite database? Or at least moving the database easily? This will just be for my own phone since it hasn't been released to the public yet.
Step #1: Add a backup/restore function to your app, that copies the database to/from the SD card. Be sure your SQLiteDatabase and SQLiteOpenHelper objects are closed first.
Step #2: Install a copy of this app, built with the old package, to your phone, and use it to back up your database.
Step #3: Install the production copy of this app to your phone and use it to restore your database.
For anybody wanting to do the same thing, I've written out how to do backup/import/restore of a database and included a whole class for doing it at this link:
http://www.hxcaine.com/blog/2010/09/14/backing-up-importing-and-restoring-databases-on-android/