I am planning to write a free version and a full version of a software. I want the information stored by the free version of the software to be accessible by the full version also (I don’t want to use Content Providers). And I also want to make sure this data is not lost when the software is updated. How do I achieve this?
Share
You need to implement an intelligent way of onUpgrade for your sqlite helpers.
You should always have the new table creation query at hand, and use that for upgrade and transfer any existing data. Note: that the onUpgrade methods runs once for your sqlite helper object and you need to handle all the tables in it.
So what is recommended onUpgrade:
if not exists(we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)List<String> columns = DBUtils.GetColumns(db, TableName);ALTER table " + TableName + " RENAME TO 'temp_" + TableName)columns.retainAll(DBUtils.GetColumns(db, TableName));)String cols = StringUtils.join(columns, ",");
)db.execSQL(String.format(
"INSERT INTO %s (%s) SELECT %s from temp_%s",
TableName, cols, cols, TableName));
DROP table 'temp_" + TableName)(This doesn’t handle table downgrade, if you rename a column, you don’t get the existing data transfered as the column names do not match).
.