Upgrading database Sugar ORM always delete previous table's data - android

I have a database with Sugar ORM, and every time I create a new class that extends SugarRecord, I need to upgrade my database_version on android manifest to Sugar ORM recognize the new table. Ok till then.
The problem is that Sugar ORM is deleting all my previous data on every table of my database! I have a lot of data on them, and I can't delete it and reinsert it on every database upgrade.
There is some way to avoid this?
Thanks

Create a folder named sugar_upgrades in your assets folder.
Create a file named <version>.sql in that folder, which corresponds to the database version you are upgrading to in Android Manifest.
eg. 1.sql, 2.sql This file would contain all the update/alter queries for that particular version.
Change the VERSION metadata field in AndroidManifest.xml to the appropriate version.
Sugar takes care of upgrading the database from its present version to the upgraded version. For eg: if the database is currently at version 1 and the upgraded version is 4, it'd look for 2.sql, 3.sql and 4.sql and execute all of the present files in that order.
Note that Sugar will automatically create tables for new entities, so your migration script only needs to cater for alterations to existing tables.
Upgrade Script sample.
This is a normal sql script file. You can add all your alter and insert/update queries, one line at a time. Each line is terminated by a ; (semicolon).
file in your assets/sugar_upgrades folder : 2.sql
alter table NOTE add NAME TEXT;

you can just use sugar orm 1.5 version like
compile 'com.github.satyan:sugar:1.5'
and then just update database version in manifest and be sure that your data doesn't wiped

Related

Android SqliteAssetHelper - merging database tables from asset with the existing one

I am using Android SqliteAssetHelper library (https://github.com/jgilfelt/android-sqlite-asset-helper)
I have shipped my database using this library and the database contains some records on table1.
Now I want to update my app with a new database with additional records which should be inserted in the one which i already shipped. I am not sure how exactly to write the SQL scripts for the upgrade since the schema is same for both the databases. Did anyone try this?
After comments by the op in other answers, it was clarified that there are two databases to merge together. The op wants to know if there is a convenient way to merge two databases together with the SQLiteAssetHelper library.
Unfortunately there isn't a direct way to do that because that library also uses the same Android pattern of running a script to modify an existing database.
The workaround is to transform the second database (set of 50 records) into 50 INSERT statements that will get put next to the existing 50. (There are various tools all over the internet to simplify that step so you don't have to do it by hand.) So as long as the business logic can work with them all together they can all go in the original table if the schemas are the same; or if you need them separated, use the 50 INSERTs still still but have them INSERT to a different table name instead.
Then, once you have these 50 INSERT statements with the data of the 50 new rows, put the statements in an upgrade script and you can follow the standard library documentation on how to get that script to run via this library.
You can make this happen by using sqldiff to find the differences between an old DB and a new one.
You call sqldiff on your two databases and pipe the output into a file that conforms to SQL Asset Helper's upgrade format (i.e. <database_name>_upgrade_<from_version>-<to_version>.sql).
So, the whole thing would be sqldiff database.db database_new.db > database.db_upgrade_1-2.sql
Then just make sure that .sql file is in the assets/databases directory and change the version numbers in your Java code (in the example case, from 1 to 2).

SQLiteOpenHelper.onUpgrade how does it exactly works? (app with multiple databases)

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.

Issue related to upgrading application related to Database in Android

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

Building of an sqlite database outside an android project?

I'd like to create a project which generates a sqlite database, which will eventually be used by an android application. I'd like to create this project as a standard java application, so I can hook it up to a build script etc. What's a good way to go about doing this, so that the sqlite database I output is conformant with the way android sqlite classes expect to have it in?
I could create this util project as an android project, and then I have access to all the sqlite classes, but the output sqlite file would live on an emulator instance, right? And I'd have to fire up an emulator etc whenever I wanted to run the util, ugh.
Thanks
As others have suggested, I wouldn't build a project for it, I'd find one of the existing utilities out there and create the DB that way. I use SQLite Expert.
Despite what Seva said, there are some things you have to do to make it usable by android. It's readable in any state, but if you want the framework to be able ot make use of it like intended (to populate listviews and other widgets), it has to have certain things.
1) The database must contain a table called "android_metadata"
2) This table must have the column "locale"
3) There should be a single record in the table with a value of "en_US"
4) The primary key for every table needs to be called "_id" (this is so Android will know where to bind the id field of your tables)
Then you put the DB in your assets folder and copy it to your apps data directory on startup.
A good link for this process is here.
Why do you want create a separate Java project to create a SQLite database? There are graphical shells over SQLite out there. I personally like SQLiteStudio.
There's nothing special about the way Android accesses them - SQLite is SQLite, the database format is the same on every platform. Create a new database file, create some tables in it, insert some data, then place it into an Android project and play with it.
can create you other as libray project and can attach it with your project...libaray project may be an android or simple java project as per your need...
Note: use the version of sqlite that comes with the SDK -- it's been modified slightly. If you use the off-the-shelf sqlite3 commandline tool, the databases it generates are incompatible with Android.

Android sqlite database access

I have made a sample.sql database with "SQL Lite Manager".
How can I access this in an Android project?
Put your prebuild database file in /assets directory in your apk,
and on first use copy to "/data/data/<application_package>/databases/" directory.
Now use it with SQLite Database Helper class in your android application...
For more info look at this Article
There are several steps to using an existing SQLite data base in an Android project. They are nicely described in this blog post by Juan-Manuel FluxĂ .
Basically, you need to make sure that the data base contains certain tables and column names, then when your program first runs, copy the data base from your assets or resources folder to the standard db location for your app. The latter step is best done by writing a DataBaseHelper class the way that FluxĂ  describes.

Categories

Resources