onUpgrade in sqlite external database in android - android

I am developing a database application and I am stuck up at one place.
How to upgrade external database after adding new table in Database in android sqlite?
after updating my app I got error: No such table found in database

In SQLiteOpenHelper class
public abstract void onUpgrade (SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

You have to Update the Version Number
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(){
super(context,DATABASE_NAME,null,VersionNumber);// Change Version here
}
public void onCreate(SQLiteDatabase db) {}
//While upgrading pass new version
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
}
Example
How to use onUpgrade() correctly

Related

SQlite "no such table" after updating application

I updated my application today and started seeing the following in crashlitics:
android.database.sqlite.SQLiteException: no such table: LESSONATTACHMENT (code 1 SQLITE_ERROR[1]): , while compiling: SELECT _THUMB_PATH FROM LESSONATTACHMENT WHERE _id = 2
I create a new table inside my SQLiteOpenHelper, it was not in the application before I update.
This is how I created the table:
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Should I instead place it in OnUpgrade? Like this:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(CREATE_TABLE_LESSON_ATTACHMENTS);
}
Whenever you want to make changes in tables or create a table on app update, you have to change the version of database. In your case let us suppose you have upgraded from dbversion from 2 to 3 and you want to create a table for versions greater than 2.Then you have to create a table in onUpgrade like below:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>=3){
db.execSQL("CREATE TABLE IF NOT EXISTS `TABLE_NAME` ....");
}
}
here I am creating the table for all the versions greater than 2 since there is a possibility that dbversion gets upgraded from 2 to 4 or some other version greater than 3.
You Need to increase your Database Version and create the new Table in onUpgrade and Maybe delete the old Table.
Your Database is created only once. Upgrades are done in the onUpgrade function
You can also delete the Database and it will be created new with the new Table, but it depends if you are able to do this at the Moment.
for example this Code will upgrade your Database up to the current Version
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
//Changes between version 1 and 2
case 2:
// Changes between Version 2 and 3
case currentversion:
//changes between last change and now
}
}
when you update your database just put a new case with the new Version below this. This way you can maintain every Version of Database and make sure that it will be upgraded properly. Important is, that you don't use a break after each case.
this Code Shows for example one of my Databases, when I increase the Version I simply add a new case for the old Version. When you havent upgraded your Database yet, set the new Version to 1 higher than your old Version and add a case clause for the old Version.
public class database extends SQLiteOpenHelper {
private static String DATABASE = "Database";
private static int VERSION = 4;
public database(#Nullable Context context) {
super(context, DATABASE, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE DATABASE TABLE");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion){
case 1:
db.execSQL("CHANGES DONE AFTER VERSION 1");
case 2:
db.execSQL("CHANGES DONE AFTER VERSION 2");
case 3:
db.execSQL("CHANGES DONE AFTER VERSION 3");
}
}

Android App update only working after deleting all local data

I am experiencing an issue with an app that is published on the play store:
After a new update is released, some users cannot use the app properly, until they delete the app and reinstall it from scratch. I cannot reproduce this issue and I don't have any error logs.
The app features an internal SQLite database, which will be wiped everytime a new update is released. Also it implements a REST client via okhttp library.
I feel like I am missing something in the update process, but I am not aware of anything else that could go wrong. It seems like there could be issues with the DB, but it will be wiped completely. Also the REST client might be a problem, but I couldn't imagine this actually being an issue. There are no other libraries that seem relevant to this problem.
I am looking forward to any suggestions, thank you,
J.V.
Edit: the code segment that is used for SQLite handling
public RfidTagDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
SQL_DELETE_ENTRIES contains a piece of SQL code that drops all tables if existing, CREATE_ENTRIES does the opposite
Your onUpgrade() must leave the database in a state appropriate for newVersion. Don't just delete the table but also create it anew. For a destructive upgrade process such as yours you can achieve it by calling onCreate() in onUpgrade():
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
You should upgrade the database Version of your class lets say from 1 to 2. In your SQliteHelper Class. Then delete entries and the create entries like:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
db.execSQL(SQL_CREATE_ENTRIES);
}

onUpgrade in sqlite database in android

I am developing a database application and I am stuck up at one place.
The scenario is: I have a database and I am maintaining the version of the database.
It is the same as that of the application version.
Now I am implementing the database in version 1. There are 3 tables getting created in version 1. In version 2, I am upgrading the database by adding one more table. Hence, the query is in the upgrade().
Now, what if the user installs version 2. The onUpgrade() will not get called because there is no database. Hence, the onCreate() will be called and the consequence will be it will create only 3 tables.
I was thinking to call the onUpgrade() explicitly in the onCreate(). But many developers on stackoverflow have suggested not to call it explicitly.
I am completely stranded.
Thanks in advance.
I know it's an old post, you have certainly found a solution, but I find the question valid (upvoted) and as it did not receive an answer, here are my 2 cents:
Calling onUpgrade() from onCreate() should work (I did not test it), but there is a better way to organize you helper (see snippet hereafter). The key point is that both those methods are just entry points.
public class SampleDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydatabase";
private static final int DB_VERSION = 2;
public SampleDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
updateDatabase(db, 0, DB_VERSION);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateDatabase(db, oldVersion, newVersion);
}
private void updateDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 1) {
// Create 3 initial tables
}
if (oldVersion < 2) {
// Create the new table
}
}
}
The advantages of this kind of helper:
You never need to change onCreate() and onUpgrade() methods, only updateDatabase().
For each new version, you just need to :
increment DB_VERSION
add a new section if (oldVersion < N) { // do the upgrade }.
Credit : Strongly inspired from Head First Android Development, Dawn and David Griffiths, O'Reilly, 2015

SQLite database add new table when upgrade app

I add one more table to my app's database.
However, if I upgrade my app without clearing its data, the new table doesn't seem to be added automatically.
Therefore, some code that need to retrieve data from the new table fail.
Is it possible to add new tables automatically on app upgrade?
Please implement onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) method.
There is an Android helper class to manage database creation and version management using an application's raw asset files.
Have a look at this :
https://github.com/jgilfelt/android-sqlite-asset-helper
Yes there is a method onUpgrade
add you code there and increase database version if your version is higher than the previous
onUpgrade is fired and the your code will execute which you write to add table
sample
Remember increasing you version code in super call
class Database extends SQLiteOpenHelper
{
public Database(Context _myContext)
{
super(_myContext, DB_NAME, null, DB_VERSION_CODE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(oldVersion > newVersion)
{
// create a new table what you want to create you can also check the existence of the table
}
}
}

Getting an Android Database Setup?

Typically, for a WinForm or a Web App, I create the database and tables through the RDBMS or through a separate install process. However, I haven't seen anything of the sort in Android. All the examples I've seen have the database creation scripts embedded in an activity like this.
The best thing I can come up with now is to call a method from the data access constructor to check whether the database is installed - if not - install it. However, this seems like a lot of overhead to me.
What's the cleanest way to execute a android database install and then forget about it?
When using SQLLiteOpenHelper (http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html), the onCreate method will be called only if the database doesn't exist. onUpgrade will be called when a new version of the database is introduced.
IF the database already exists, and no version upgrade occured, these methods won't be executed.
There is no need for implementing if-else checks in your activity.
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
db.execSQL(DATABASE_UPGRADE);
}
}

Categories

Resources