Nirav Bhandari
Nirav Bhandari

Reputation: 4610

Upgrade SQLiteDatabase Android

I am writing application which uses SQLite. At first, my database had only one table called products. Now I want to add one more table called favouriteproduct_table. I wrote db.exec("create table fav_product") command in the onCreate method, but my app is crashing saying that no table found for Product_fav.

This is my code before adding favourite_product_table.

   private static class OpenHelper extends SQLiteOpenHelper 
   {

      OpenHelper(Context context) 
      {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) 
      {

          db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, systemSerID TEXT, systemSerName TEXT, productID TEXT, productName TEXT, productDesc TEXT, productType TEXT, batchID TEXT, minVal TEXT, maxVal TEXT)");

      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }

   }

Above code is working fine, but when I try to create a new table using the following code, my app crashes.

 private static class OpenHelper extends SQLiteOpenHelper 
   {

      OpenHelper(Context context) 
      {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) 
      {

          db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, systemSerID TEXT, systemSerName TEXT, productID TEXT, productName TEXT, productDesc TEXT, productType TEXT, batchID TEXT, minVal TEXT, maxVal TEXT)");
          db.execSQL("CREATE TABLE " + TABLE_TOPUP_FAVOURITE + "(id INTEGER PRIMARY KEY, systemSerID TEXT, systemSerName TEXT, productID TEXT, productName TEXT, productDesc TEXT, productType TEXT, batchID TEXT, minVal TEXT, maxVal TEXT, imageid TEXT)");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }

   }

Upvotes: 0

Views: 371

Answers (1)

Dharmendra
Dharmendra

Reputation: 33996

Check your question what you have asked

Now I want to add one more table called favouriteproduct_table

and then

db.exec("create table fav_product")

and at last

my app is crashing saying that no table found for Product_fav

Now see what is the error.

In three of your sentence tables are of different names.

DO you have all these tables(favouriteproduct_table, fav_product, Product_fav) different which each others?

Upvotes: 1

Related Questions