I'm new to writing Xamarin Android by about 8 months.
I have an application up on the Google Play Store and when the application first installs, it creates the sqlite database from code.
I had to make a database table change, I added a few columns.
When the application does it's auto update, where/how do I tell it to drop the database and recreate it, or is it automatic? I'd expect not.
I'm not worried about the data, it will be re-downloaded.
I looked at Assets, creating a text file and reading a command from it, but I can't remove the asset, so that was a dead end.
Thanks for any help.
I like to use the built-in SQLiteOpenHelper class to maintain the Sqlite version numbers and providing the process for upgrading the database.
Using the builtin Android.Database.Sqlite.SQLiteOpenHelper you can easily use it to maintain your database version and if the version changes, delete the database and start fresh with a blank one (no tables/no data).
Remember: This is destructive "upgrade"...
public class SqliteOnVersionChangeCreateBlankDB : SQLiteOpenHelper
{
new const string DatabaseName = "myDBName";
const int DatabaseVersion = 1;
public SqliteOnVersionChangeCreateBlankDB(Context context) : base(context, DatabaseName, null, DatabaseVersion)
{
}
public override SQLiteDatabase ReadableDatabase
{
get
{
try
{
return base.ReadableDatabase;
}
catch
{
File.Delete(DatabaseName);
return base.WritableDatabase;
}
}
}
public override void OnCreate(SQLiteDatabase db)
{
Console.WriteLine($"{db}");
// You can create the DB tables/data here if needed...
// or use your favorite SQLite framework/library later...
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Console.WriteLine($"{db}:{oldVersion}:{newVersion}");
if (oldVersion < newVersion)
{
// Normally this is where you would alter the existing schema to the new version
// but this is a destructive upgrade.
db.Close();
File.Delete(db.Path);
}
}
}
Usage:
Starting with a version of 1 in the SQLiteOpenHelper subclass:
const int DatabaseVersion = 1;
Execute this code:
var sqliteHelper = new SqliteOnVersionChangeCreateBlankDB(this);
var db = sqliteHelper.ReadableDatabase;
db.Close();
You now have a blank database that is assigned version 1. It is empty, no tables/data so use your favorite Sqlite ORM/framework/library to create the tables and populate it with data...
Unless the database version changes, executing this code each time your app starts will not delete the existing database:
var sqliteHelper = new SqliteOnVersionChangeCreateBlankDB(this);
var db = sqliteHelper.ReadableDatabase;
db.Close();
Later on, App is updated and you also need to change the database, so assign a new version number that is higher the last, 2 in this case, to the database.
const int DatabaseVersion = 2;
Execute the same code as before:
var sqliteHelper = new SqliteOnVersionChangeCreateBlankDB(this);
var db = sqliteHelper.ReadableDatabase;
db.Close();
You now have a blank database again, but it is assigned version 2. Again your favorite Sqlite ORM/framework/library to create the tables and populate it with data...
First you might want to confirm your intended behavior. I would track your .sqlite file to see what happens to it given your workflow.
For the most part when an application is being updated from Google Play Store, all of your application's data will stay put. (As it is only updating and not uninstalling -> installing again)
You can mimic this behavior by applying the following in your debug environment:
If you find yourself in a scenario where you need to update the database to associate it with a new application update, then you would need some mechanism to compare your database version and if it is older than the application's code expects, you would then apply a schema change and potentially seed the database with initial information.
Related
I'm trying to implement version control into my database. I have following code:
if (File.Exists (dbPath)) {
database = new SQLiteConnection(dbPath);
dbVersion = GetDatabaseVersion();
if (dbVersion != DATABASE_VERSION) {
CreateDatabase(dbPath);
database = new SQLiteConnection(dbPath);
SetDatabaseVersion(DATABASE_VERSION);
}
} else {
CreateDatabase(dbPath);
database = new SQLiteConnection(dbPath);
SetDatabaseVersion(DATABASE_VERSION);
}
When I run the code for the first time File.Exists (dbPath) returns false as expected. When I then stop the app and build and deploy again, File.Exists (dbPath) returns true (as expected) and the versions get checked. But when I change an entry in the database (not the version) and I build and deploy again File.Exists (dbPath) returns false. So I guess the sqlite-file gets deleted when it's changed?
What I want to achieve is that when the sqlite-file is changed, but the version numbers are still the same, the database of the app isn't updated. Only when the versions don't match, it has to be updated.
(It works perfectly for iOS this way, by the way)
Can anyone help me?
Thanks in advance.
It's difficult, without access to your debugger, to answer what's going wrong. However, your way of creating and updating your database looks unusual and unnecessarily complicated. You should consider using SQLiteOpenHelper, which will allow you a structure like this:
public class MyDatabase extends SQLiteOpenHelper {
private static final int VERION = ...;
public MyDatabase(Context context) {
super(context, "mydatabasename", null, VERION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table...");
...
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, intNewVersion) {
db.execSQL("...") // whatever you have to change from one version to another
}
}
This works without losing any database files.
You should tick this box in Visual Studio / Tools / Options / Xamarin / Android settings
I have uploaded an apk (version 1.0) with 22 tables in SQLite Database on Google Playstore.
Now i want to update database with 36 tables in new version (version 2.0) of application.
I am storing datebase at default location so when i press "Clear
data" in Application Manager, database is gonna deleted.
I just want to know how to delete old database (same as clear data) when user update new version?
Update:
If is there any solution for clear data while updating application from play store then also answered me.
Your help would be appreciated.
Thanks.
you can use this method to delete your database.
context.deleteDatabase(DATABASE_NAME);
You can also use this method to find your database path first and then delete that.
File myDb = context.getDatabasePath(DATABSE_NAME);
Boolean isDelete = myDb.delete();
The other solution is , if you want to update your database then just change your version number of database. onUpgrade() will automatically get called and your old database will be deleted and new database will be created.
Finally done with simple solution:
/** UPGRADE DATABASE **/
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "Database Version: OLD: "+ oldVersion + " = NEW: "+newVersion);
if(context.deleteDatabase(DATABASE_NAME))
Log.i(TAG, "Database Deleted....");
else
Log.i(TAG, "Database Not Deleted..");
}
Nice Question. Just follow the steps.
1) There are two methods that you override on Your Helper class
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion!=newVersion){
// Remove all old tables and crate new ones
}
}
2) create an object of Helper class.
MyOpenHelper dbHelper = new MyOpenHelper(this,"dbname", 1);
3) Now just increase the database version when you want to change the database.(Third argument in MyOpenHelper class. "1" is a database version).
4) When database version changes it will give you a callback in onUograde() method.
5) Remove all tables in onUpgrade() method and create new ones.
6) That's it.
EXPLANATION :
Your app version 1.1 is on google play and your database version is "1". Now you want to upload version 1.2 with new database. Just set database version "2" in your helper file. And check the the oldVersion and newVersion in onUpgrade() method if both are not same that means application updating and remove old tables and create new ones.
If you are using SQLiteOpenHelper just delete your tables in your onUpgrade method and recreate all your tables again. You should have not problems at all.
Check this out.
Hope it helps
Note: Be careful with database versions, if you are playing with your database numbers make sure you have the right number as your oldVersion or it will not work properly when you update your google play.
The obvious solution is to delete the old DB file when onUpgrade() is called and copy the new version over. Too bad it does not work. You get an error something like this:
Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
SQLiteLog﹕ (1032) statement aborts at 4: [PRAGMA user_version = 2]
SQLiteLog﹕ (28) file unlinked while open: /data/data/my.package/databases/my_db.db
You don't need to delete the database, just copy over it using the method you've alady defined (copyDataBase), like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();
if (oldVersion < newVersion) {
Log.v("Database Upgrade", "Database version higher, upgrading");
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error upgrading database");
}
}
}
wrok for me, thank
My app used ormlite to communicate with Sqlite database. There are many screens that get data from ormlite. The problem is I want to update the database in background without user' knowledge (it's transparent to user) and my app use some CursorAdapters and Loaders to get data. The behavior look like this: my app check if there is new version in server, it will download new db file from server, then change current db file from local by new server db file in ormlite helper. While update progress is happening, user can change screen or view data from db normally (I just use the select query)
I have searched some answer, they advised use onUpgrade method, but how can I apply this in my case, because I want to change completely the database file? And how can I switch the database while selecting data?
Here is my db helper:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public DatabaseHelper(Context context){
super(context, "document.sqlite", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Category.class);
TableUtils.createTable(connectionSource, Book.class);
} catch (Exception ex) {
Ln.e(ex);
}
}
#Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion,
int newVersion) {
try{
TableUtils.dropTable(connectionSource, Category.class, true);
TableUtils.dropTable(connectionSource, Book.class, true);
// get new one
onCreate(db, connectionSource);
}catch(Exception ex){
Ln.e(ex);
}
}
Any help would be appreciated!
I had similar situation in my project in which I had to refresh database with new data after checking for new data.After trying so many methods Finally I reached at one solution.I do not know if that solution is optimal one but after testing it is working fine.
Solution is as follows:-
I am getting new data in json form so I am creating a new blank database with temp_original-database-name.db and then populating it with new data(in your case you have complete database downloading from server , so you do not need extra helper for creating temporary database). After this data is populated then I am renaming this database temp_original-database-name.db to original-database-name.db using file system methods.
I have done testing and original helper instance used for original database still exists even after still second rename of database operation.and everything works fine.
Hope this can help.Please tell me if this helps and if you have any other question regarding this.
Is it possible to delete any existing data set by a previous install of same app (databse tables and shared preferences etc) when the app is re-installed?
I have an app that stores some values in sqlite database, if the app is re-installed without prior properly uninstalling. I face problems from previous database entries etc.
If uninstalling the app didn't do the stuff try this :
System Parameters -> Manage Applications -> Your Application -> Clear data
If you click on the button (Clear data) you will see a dialog which shows you what kind of data will be cleared.
Edit:
If you want to do that programmatically, you can :
Change database version in the super method of the constructor:
super(context, DATABASE_NAME, null, NEW_DB_VERSION);
Add a drop statement before creating tables.
database.execSQL("DROP TABLE IF EXISTS your_table");
database.execSQL(" CREATE TABLE your_table ...");
Proceed to a hard drop of the database:
this.context.deleteDatabase(YOUR_DATABASE_NAME;
Its very Simple.
First Delete the table using drop query
sdb.execSQL("DROP TABLE IF EXISTS tablename");
and then again use the create table query
sdb.execSQL(" CREATE TABLE tablename(col1 TEXT PRIMARY KEY)");
or
delete the DB file using file explorer in path data->data->package->databases->dbname
update the Database version to greater value in the OpenHelper, it will automatically drop all the tables in database and recreate them.
For shared preferences.. you can clear them from the OpenHelper when onUpgrade is called.
Like Ankit Popli said, Using version is the right way to go:
public class Database extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "YourDBName.db";
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// ALl the deletion operation here.
db.execSQL("DROP TABLE IF EXISTS "+Database.TABLE);
onCreate(db);
}
}
AndroidSqlite will automatically call onUpgrade function when the version number is incremented. Do all the database deletion there.
I have an app released on the android market which uses sqlite and displays data. I want to know if I am sending an update for the app by adding features, should I upload a new database? What happens when users already having my app, click on update? Will the database update on its own? If the users are downloading my app for the first time, they will certainly get the new db content...but what about the existing users?? I want to know if I have to explicitly update the database in the program
When you create your new version... If you change the version of the database... The onUpgrade function will run on all the existing users:
public static final int dbVersion = 2;
protected static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
#Override
public void onCreate(SQLiteDatabase db) {
//create tables
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//what do you want to do to existing users? maybe recreate?
}
}
Why not create the sqlite database through code? That way your updates can update the SQL on the database (alter columns, add rows) without affecting the users existing data.
If you are changing database's attributes then on updation it will create problem and if the database's attributes are same then it will not have any effect...
You may use android.database.sqlite.SQLiteOpenHelper class it supports versions and migration mechanism