简体   繁体   English

升级SQLiteDatabase Android

[英]Upgrade SQLiteDatabase Android

I am writing application which uses SQLite. 我正在编写使用SQLite的应用程序。 At first, my database had only one table called products . 刚开始,我的数据库只有一个表,称为products Now I want to add one more table called favouriteproduct_table . 现在,我想再添加一个名为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 . 我在onCreate方法中写了db.exec("create table fav_product")命令,但是我的应用程序崩溃了,没有为Product_fav找到任何表。

This is my code before adding favourite_product_table. 这是我添加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 现在,我想再添加一个名为favouriteproduct_table的

and then 接着

db.exec("create table fav_product ") db.exec(“创建表fav_product ”)

and at last 最后

my app is crashing saying that no table found for Product_fav 我的应用程式当机,说找不到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? 所有这些表( favouriteproduct_tablefav_productProduct_fav )是否彼此不同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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