0

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

我已尝试在此处遵循以下此示例,这似乎是我正在寻找的内容,但我的问题是该示例是所有在代码中完成的版本控制,还是我必须将版本号附加到我的 *. 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)
{
}
4

1 回答 1

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。

于 2012-09-08T16:41:23.547 回答