Android SQLite数据库不使用预先填充的sqlite数据库进行更新必须卸载并重新安装

时间:2012-09-08 14:09:30

标签: android sqlite

这是我的问题。我有一个已上传到Google Play商店的应用。该应用程序正常工作,直到它到达我在我的数据库中为我在预先填充的sqlite数据库中创建的级别添加新记录的部分,当我到达此特定级别时它崩溃。就像没有创建新信息一样。代码如下。我已经读过,我需要在onupgrade()方法中创建一个版本变量并递增它。但我是这个sqlite学习的新手。如果我创建一个版本变量代码会跟踪它吗?我是否必须用版本号重命名数据库,请帮忙。

我尝试过以下这个示例here,它似乎正是我要找的,但我的问题是看到的例子是所有在代码中完成的版本控制或我是否必须追加我的* .db文件的版本号。

package com.xtremeware.straighthoodtrivia.db;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.xtremeware.straighthoodtrivia.quiz.Question;

public class DBHelper extends SQLiteOpenHelper
{   
// The androids's default system path of the application database.
private static String DB_PATH = "/data/data/com.xtremeware.straighthoodtrivia    /databases/";
private static String DB_NAME = "QuestionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;

// Constructor
// Takes and keeps a reference of the passed context in order to access to the 
// applications assets and resources.
// @ context

public DBHelper(Context context)
{
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

// Creates a empty database on the system and rewrites it with my own database
public void createDataBase() throws IOException
{
    boolean dbExist = checkDataBase();
    if(!dbExist)
    {
        // By calling this method an empty database will be created into the default system path
        // of my application so I am gonna be able to overwrite the database with my database.
        this.getReadableDatabase();
         try 
         {
             copyDataBase();
         }
         catch (IOException e)
         {
             throw new Error("Error copying database");
         }
    }
}

// Check if the database already exists to avoid re-copying the file each time you open the application.
// @return true if it exists, false if it doesn't

private boolean checkDataBase()
{
    SQLiteDatabase checkDB = null;
    try
    {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }
    catch (SQLiteException e)
    {
        // Database doesn't exist yet.
    }
    if(checkDB != null)
    {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

// Copies the database from your local assets-folder to the just created empty database in 
// the system folder, from where it can be accessed and handled.
// This is done by transferring bytestreams.

private void copyDataBase() throws IOException
{
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the empty db just created
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the output file
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0)
    {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException
{
    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close()
{
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db)
{
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}

1 个答案:

答案 0 :(得分:1)

你输入的内容如下:

private static final int DATABASE_VERSION = 2;

在您的DBHelper类中。

然后是构造函数的实现,以及onCreate和onUpgrade方法,如:

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

   @Override
   public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table existing_table...");
      db.execSQL("create table new_table...");
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
      //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
      if (oldVersion == 1 && newVersion >= 2){
         db.execSQL("alter table existing_table add column new_column integer not null default -1;");
         db.execSQL("create table new_table (...)");
      }
   }

版本控制在代码中完成,但版本代码在DBHelper构造函数的super(...)调用中传递,它负责确保数据库知道其当前版本。您不必将版本号自己附加到“* .db”文件中。

基本上,通过比较oldVersion和newVersion,您可以决定在升级时运行哪些脚本。 但是,对于新的应用安装,您只需确保onCreate版本立即创建最新的表。

下次要发布需要更新表结构的应用版本时,可以增加DATABASE_VERSION字段的值,并根据需要更改onUpgrade。