简体   繁体   中英

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);
      }

   }

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM