I have an app receiving its data from a database in the assets folder. Now everyone has the app on their phones I want to update the database. I have the new database ready to go on a server, but how to I get the updated database into the assets folder on their phones. All only if they want to update of course???
Cheers,
Mike.
SQLiteOpenHelper constructor has an version argument. If you supply some number that is bigger than the existing database version in phones memory, onUpgrade function will run after getWritableDatabase is called. Inside onUpgrade you should handle updating your database.
Some example code here:
Question about onUpgrade method android
Related
I have a database stored in my assets folder that I built using SQLite Manager. Now, when I add new records to any table of the database, I have to uninstall and reinstall the app. What would I have to do to upgrade the database in the right way without uninstalling the app?
Note:
My onCreate() and onUpgrade() are empty because as I mentioned, the database is already built.
I call super(context, DB_NAME, null, 1); in my helper constructor, but changing the version would not do anything because onUpgrade() is empty.
To allow upgrading, you must implement onUpgrade and do whatever is necessary to convert the old version to the new version.
If you have the new database file in the assets folder, you must replace the old file.
This is not possible with SQLiteOpenHelper because it has an active transaction while onUpgrade is called, but when you are not actually using the automatic creation/versioning mechanism, there is not reason to use SQLiteOpenHelper in the first place.
I have Database in my application which is created by just coping the SQLite database file from the assets folder but now in the next release I want to update the database without losing the previous data.
Is there any Logic or way to copy the previous data and create a new database through SQLiteOpenHelper so that in near future I will not face this problem.
Thanks
Use this gist
In the OnUpgrade method, copy your previous data from the old database.
In the doUpgrade method, use your copied data to update the new database.
Previous data will be kept by default when users update to a new version. but you must keep in mind there will be new users never get a former version app. So you should add complete database in the assets folder.
I have an app that creates a database and do some stuff. I am wondering if i upload a new db to a server and download it to the exact folder where the older one exists it will be overwritten and i am good to go? Or there will be a problem. Assuming it has the same name, same column names, etc. Of course i am reffering to sqlite.
In Android, when performing a database update you should be using onUpgrade inside of the SQLiteOpenHelper. One way of doing this is to download text files that include the sql instructions needed to modify the current database or update rows with new data. The reason you have to do this is because Android will only create the database once. After the initial creation the call to onCreate for the database will not occur.
I hava an android application which consists sqlite database in the assets folder.
In the DB I have several tables, which one of them is user data (which is updated over time by using the application - when the user installs the application this table is empty).
The other tables store data that I update.
The question is: when a user gets an updated version of my application (with sqlite database in the assets folder) from the market, I need to keep the data the user updated by using the application, but i do want to update the other tables (which consist my data).
What is the correct way to do it?
Thank You :)
Keep a version number for each change and implement the onUpgrade method for the possible combinations. See more in the javadoc for SQLiteOpenHelper
Since you said your tables are empty when the Database is first created, it shouldn't be necessary to add the Database from the /assets-folder.
Instead, you can use a SQLiteOpenHelper, which offers an onCreate()-method that can do the initial table-creation (an add some example data if necessary).
If you then update your app, you simply increase the Database-version and the onUpgrade()-method is called, where you can then perform the Database update.
See this other question: Run some code when user update my app
If your app comes with a huge Database and inserting entry's in the SQLiteOpenHelper isn't the right way to go, you can still check if the Database already exists and then do the updating (through the onUpgrade()-method) and keep the users data.
I have a database that I built in SQLite browser, and it works fine. I launched an app with a prebuilt database and now I want to add more tables and data to that database.
I can get the app to launch the onUpgrade() method of the SQLiteOpenHelper. But the problem is, it's doing that EVERY time I use the helper.
I have it localized to, only on app launch, separating the upgrade command from the helper I used to retrieve data, but this is still a problem.
I have figured it out though, as I have been using the same database on my computer (the one that I'm editing) since version 1. So, whenever it writes the newer database onto the SD card it's showing version 1 even though I should be up to version 4 by now.
So, my question is, how can I manually edit the database version of the original database so that when it updates it isn't writing the old version number over the new one?
To manually update the version to 4 you execute the following SQL statement:
PRAGMA user_version = 4
Another way to change the version of your Sqlite Database. You can use DB Browser for SQLite:
Open the database file with "DB Browser for SQLite".
Change the "User Version" number to whatever number you want
Click the "Save" button
You can also set it via the setVersion SqlLiteDatabase method.
Source: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setVersion(int)