Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 升级我的SQLite数据库_Android_Sqlite_Android Sqlite - Fatal编程技术网

Android 升级我的SQLite数据库

Android 升级我的SQLite数据库,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我是SQLite的新手。 如何升级数据库 我以前的数据库版本中有7个问题。 今天我添加了大约13个新的,总共20个。 我使用addQuestion方法添加了它们。 但是,它不起作用 我一直在修补我的onUpgrade。 我做错了什么事 public class QuizHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final Stri

我是SQLite的新手。
如何升级数据库

我以前的数据库版本中有7个问题。
今天我添加了大约13个新的,总共20个。
我使用addQuestion方法添加了它们。
但是,它不起作用

我一直在修补我的onUpgrade。
我做错了什么事

public class QuizHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "creativequestion";
    private static final String TABLE_QUEST = "quest";
    private static final String KEY_ID = "qid";
    private static final String KEY_QUEST = "question";

    private SQLiteDatabase dbase;



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

    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( " + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUEST + " TEXT)";
        db.execSQL( sql );
        addQuestion();


    }

    private void addQuestion() {

        QuestionFaci q1 = new QuestionFaci( "OTHER USES: Name other uses of a Hammer. \n\n Example: Stir a soup." );
        this.addQuestion(q1);
        QuestionFaci q2 = new QuestionFaci( "RHYMES: Words that rhymes with Rice. \n\n Example: Ice" );
        this.addQuestion(q2);
        QuestionFaci q3 = new QuestionFaci( "WITH: I can cook eggs with... \n\n Example: A Piece of Plywood" );
        this.addQuestion(q3);
        QuestionFaci q4 = new QuestionFaci( "WITHOUT: I can wash my clothes without... \n\n Example: My Aunt" );
        this.addQuestion(q4);
        QuestionFaci q5 = new QuestionFaci( "I WILL: If I was Bill Gates, I will... \n\n Example: Buy a spaceship" );
        this.addQuestion(q5);
        QuestionFaci q6 = new QuestionFaci( "CREATE A MOVIE TITLE: A NIGHT \n\n Example: To Remember" );
        this.addQuestion(q6);
        QuestionFaci q7 = new QuestionFaci( "OTHER NAMES: Other names of a cow \n\n Example: Milk giver" );
        this.addQuestion(q7);
        QuestionFaci q8 = new QuestionFaci( "OTHER USES: Name other uses of a Cowboy Boots. \n\n Example: Pound a nail." );
        this.addQuestion(q8);
        QuestionFaci q9 = new QuestionFaci( "RHYMES: Bake a. \n\n Example: Flake." );
        this.addQuestion(q9);
        QuestionFaci q10 = new QuestionFaci( "I WILL: I will drive a helicopter \n\n Example: with eyes closed" );
        this.addQuestion(q10);
        QuestionFaci q11 = new QuestionFaci( "CREATE A TITLE: The Greatest \n\n Example: Heist" );
        this.addQuestion(q11);
        QuestionFaci q12 = new QuestionFaci( "CHANGE AN INGREDIENT: --- Cookie \n\n Example: Orange Chip" );
        this.addQuestion(q12);
        QuestionFaci q13 = new QuestionFaci( "FINISH ME: Can you remember the times when.. \n\n Example: push me over a cliff" );
        this.addQuestion(q13);
        QuestionFaci q14 = new QuestionFaci( "OTHER NAMES: Ball bearings \n\n Example: Mommy's new bangles" );
        this.addQuestion(q14);
        QuestionFaci q15 = new QuestionFaci( "FINISH ME: The donut rolls \n\n Example: down the hill" );
        this.addQuestion(q15);
        QuestionFaci q16 = new QuestionFaci( "RHYMES: Flip the \n\n Example: clip" );
        this.addQuestion(q16);
        QuestionFaci q17 = new QuestionFaci( "OTHER NAMES: Police \n\n Example: civilian peacekeeper" );
        this.addQuestion(q17);
        QuestionFaci q18 = new QuestionFaci( "CREATE A TITLE: Go Go \n\n Example: Changin" );
        this.addQuestion(q18);
        QuestionFaci q19 = new QuestionFaci( "I WILL: I will distribute screwdrivers \n\n Example: to all drivers" );
        this.addQuestion(q19);
        QuestionFaci q20 = new QuestionFaci( "CREATE A DISH: Chicken --- Soup \n\n Example: Dumpling" );
        this.addQuestion(q20);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        String upgradeQuery = "ALTER TABLE quest ADD COLUMN question TEXT";
        if(newV>oldV)
            db.execSQL( upgradeQuery );
        onCreate( db );


    }

    private void addQuestion(QuestionFaci quest) {
        ContentValues values = new ContentValues(  );
        values.put(KEY_QUEST, quest.getQUESTION());
        dbase.insert( TABLE_QUEST, null, values );
    }


    public List<QuestionFaci> getAllQuestions(){
        List<QuestionFaci> quesList = new ArrayList<QuestionFaci>(  );
        String selectQuery = "SELECT * FROM " + TABLE_QUEST;
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery( selectQuery, null );
        if (cursor.moveToFirst()){
            do{
                QuestionFaci quest = new QuestionFaci(  );
                quest.setID( cursor.getInt( 0 ) );
                quest.setQUESTION( cursor.getString( 1 ) );

                quesList.add(quest);
                } while (cursor.moveToNext());
         }

        return quesList;

    }
}
public类QuizHelper扩展了SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
私有静态最终字符串数据库\u NAME=“creativequestion”;
私有静态最终字符串表_QUEST=“QUEST”;
私有静态最终字符串密钥\u ID=“qid”;
私有静态最终字符串密钥\u QUEST=“question”;
专用数据库数据库;
公共QuizHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
dbase=db;
String sql=“如果不存在创建表”+表任务+”(“+KEY\u ID+”整型主键自动递增“+KEY\u任务+”文本)”;
execSQL(sql);
添加问题();
}
私人问题{
QuestionFaci q1=new QuestionFaci(“其他用途:命名锤子的其他用途。\n\n示例:搅拌汤”);
这个问题(q1);
QuestionFaci q2=新的QuestionFaci(“押韵:与Rice押韵的单词。\n\n示例:Ice”);
这个问题(q2);
QuestionFaci q3=新的QuestionFaci(“使用:我可以使用…\n\n示例:一块胶合板来煮鸡蛋”);
这个问题(第三季度);
QuestionFaci q4=新的QuestionFaci(“没有:我可以不洗衣服…\n\n示例:我的阿姨”);
这个问题(第四季度);
QuestionFaci q5=新的QuestionFaci(“我会:如果我是比尔·盖茨,我会…\n\n示例:买一艘宇宙飞船”);
这个问题(q5);
QuestionFaci q6=新建QuestionFaci(“创建电影标题:夜晚\n\n示例:记住”);
这个问题(q6);
QuestionFaci q7=新的QuestionFaci(“其他名称:奶牛的其他名称\n\n示例:送奶人”);
这个问题(q7);
QuestionFaci q8=新的QuestionFaci(“其他用途:命名牛仔靴的其他用途。\n\n示例:敲打钉子”);
这个问题(q8);
QuestionFaci q9=新的QuestionFaci(“押韵:烘焙a.\n\n示例:薄片”);
这个问题(q9);
QuestionFaci q10=新的QuestionFaci(“我将:我将驾驶直升机\n\n示例:闭上眼睛”);
本.补充质询(q10);
QuestionFaci q11=新的QuestionFaci(“创建一个标题:最伟大的\n\n示例:抢劫”);
本问题(q11);
QuestionFaci q12=新的QuestionFaci(“更改成分:---饼干\n\n示例:橙色芯片”);
本条问题(q12);
QuestionFaci q13=新的QuestionFaci(“完成我:你还记得….\n\n示例:将我推下悬崖的时间吗”);
本问题(第13题);
QuestionFaci q14=新的QuestionFaci(“其他名称:滚珠轴承\n\n示例:妈妈的新手镯”);
本.补充质询(问题14);
QuestionFaci q15=新的QuestionFaci(“完成我:甜甜圈滚动\n\n示例:下山”);
本.补充质询(第15题);
QuestionFaci q16=新的QuestionFaci(“押韵:翻转\n\n示例:剪辑”);
本.补充质询(第16题);
QuestionFaci q17=新的QuestionFaci(“其他名称:警察\n\n示例:文职维和人员”);
本.补充质询(第17题);
QuestionFaci q18=新建QuestionFaci(“创建标题:Go-Go\n\n示例:Changin”);
本.补充质询(第18题);
QuestionFaci q19=新的QuestionFaci(“我将:我将分发螺丝刀\n\n示例:所有螺丝刀”);
本.补充质询(第19题);
QuestionFaci q20=新的QuestionFaci(“创建一道菜:鸡肉---汤\n\n示例:饺子”);
这个问题(q20);
}
@凌驾
public void onUpgrade(SQLiteDatabase db、intoldv、intnewv){
String upgradeQuery=“ALTER TABLE quest ADD COLUMN quest TEXT”;
如果(新值>旧值)
execSQL(升级查询);
onCreate(db);
}
私人问题(提问){
ContentValues=新的ContentValues();
value.put(KEY_QUEST,QUEST.getQUESTION());
insert(TABLE_QUEST,null,value);
}
公共列表getAllQuestions(){
List quesList=newarraylist();
String selectQuery=“SELECT*FROM”+表格任务;
dbase=this.getReadableDatabase();
Cursor Cursor=dbase.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
做{
QuestionFaci quest=新的QuestionFaci();
setID(cursor.getInt(0));
setquest.setQUESTION(cursor.getString(1));
添加(任务);
}while(cursor.moveToNext());
}
返回任务列表;
}
}

您应该将
数据库版本
变量增加到例如2。这将调用
onUpgrade
方法

这不是最好的方法,但如果当前数据库的版本更高,您可以直接删除表,然后调用
onCreate()


如前所述,如果保存的数据很简单,则可以擦除数据库,然后重新创建它。如果有其他数据,可能是用户结果、用户进度或其他任何数据,则需要在onUpgrade中正确处理更新

我通常做的是使用onUpgrade从android资源中读取迁移文件。这些文件将具有数据库模式更改(如果需要)和在版本之间迁移数据库所需的任何更新语句。我宁愿不要下面的case语句,也不要动态地寻找迁移,但这是一个必要的缺点,允许我在必要时定制和调整每个迁移

@Override
   public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion)
   {
      AppLog.i(TAG, "onUpgrade, oldVersion=["+oldVersion+"], newVersion=["+newVersion+"]");
      try 
      {
          // Simply loop round until newest version has been reached and add the appropriate migration
          while (++oldVersion <= newVersion) 
          {
              switch (oldVersion) 
              {
              // The version in the case statement is the new (target) version. So case 2: will be an upgrade from 2 to 3.  
                  case 2: {
                      UpgradeUtils.addUpgrade(oldVersion);
                      break;
                  }
                  case 3: {
                      UpgradeUtils.addUpgrade(oldVersion);

                      break;
                  }
                  case 7: {
                     UpgradeUtils.addUpgrade(oldVersion);
                  }

                  case 8: {
                     UpgradeUtils.addUpgrade(oldVersion);
                  }
              }
          }
          // Get all the available updates
          final List<String> availableUpdates = UpgradeUtils.availableUpdates(DatabaseHelper.context.getResources());
          AppLog.d(TAG, "Found a total of " + availableUpdates.size() +" update statements" );

          for (final String statement : availableUpdates) 
          {
              db.beginTransaction();
              try {
                  AppLog.d(TAG, "Executing statement: " + statement);
                  db.execSQL(statement);
                  db.setTransactionSuccessful();
              }
              finally {
                  db.endTransaction();
              }
          }
      }
      catch (Exception e)
      {
         Log.e(TAG,"Error executing sql while upgrading database", e);
      }

   }
@覆盖
公共空间升级(SQLi)
@Override
   public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion)
   {
      AppLog.i(TAG, "onUpgrade, oldVersion=["+oldVersion+"], newVersion=["+newVersion+"]");
      try 
      {
          // Simply loop round until newest version has been reached and add the appropriate migration
          while (++oldVersion <= newVersion) 
          {
              switch (oldVersion) 
              {
              // The version in the case statement is the new (target) version. So case 2: will be an upgrade from 2 to 3.  
                  case 2: {
                      UpgradeUtils.addUpgrade(oldVersion);
                      break;
                  }
                  case 3: {
                      UpgradeUtils.addUpgrade(oldVersion);

                      break;
                  }
                  case 7: {
                     UpgradeUtils.addUpgrade(oldVersion);
                  }

                  case 8: {
                     UpgradeUtils.addUpgrade(oldVersion);
                  }
              }
          }
          // Get all the available updates
          final List<String> availableUpdates = UpgradeUtils.availableUpdates(DatabaseHelper.context.getResources());
          AppLog.d(TAG, "Found a total of " + availableUpdates.size() +" update statements" );

          for (final String statement : availableUpdates) 
          {
              db.beginTransaction();
              try {
                  AppLog.d(TAG, "Executing statement: " + statement);
                  db.execSQL(statement);
                  db.setTransactionSuccessful();
              }
              finally {
                  db.endTransaction();
              }
          }
      }
      catch (Exception e)
      {
         Log.e(TAG,"Error executing sql while upgrading database", e);
      }

   }
public class UpgradeUtils
{
   private static final String TAG = "UpgradeHelper"; 

   protected static final Set<Integer> mVersionSet;
   static 
   {
      mVersionSet = new LinkedHashSet<Integer>();
   }

   public static final void addUpgrade(final int version) 
   {
      AppLog.d(TAG, "Adding " + version + " to upgrade path");
      mVersionSet.add(version);
  }

   public static List<String> availableUpdates(final Resources resources) 
   {
      final List<String> updates = new ArrayList<String>();

      for (Integer version : mVersionSet) 
      {
              // Migration files are kept in assets/updates/migration-X.sql
          String fileName = String.format("updates/migration-%s.sql", version);

          AppLog.d(TAG, "Adding db version [" + version + "] to update list, loading file ["+ fileName +"]" );

          final String sqlStatements = DBUtils.loadAssetFile(resources, fileName);

          final String[] splitSql = sqlStatements.split("\\r?\\n");
          for (final String sql : splitSql) 
          {
              if (DBUtils.isNotComment(sql)) 
              {
                  updates.add(sql);
              }
          }
      }
      return updates;
  }