0

你可以在下面看到我的数据库助手类。我使用在 assets 文件夹中导入的预填充 sqlite 数据库。每当我将表添加到现有数据库时,如果我的应用程序已安装在手机上,则不会出现此类表错误。我想我的 onUpgrade() 方法现在很好。它有效,不要误会我的意思,当我将一些数据更改为现有表时,我会增加数据库版本并更新它。但是如果我添加一个表,我会得到错误。

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "/data/data/rs.androidaplikacije.themostcompleteiqtest/databases/"; 
private static String DB_NAME ="pitanja.sqlite";// Database name
private static SQLiteDatabase mDataBase; 
private final Context mContext;
private static final int DATABASE_VERSION = 3;

public DataBaseHelper(Context mojContext) 
{
    super(mojContext, DB_NAME, null, 3);// 1 it's Database Version
    DB_PATH = mojContext.getApplicationInfo().dataDir + "/databases/";
    this.mContext = mojContext;
}

public void createDataBase() throws IOException
{
    //If database not exists copy it from the assets


        this.getReadableDatabase();
        this.close();
        try 
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
public boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}
    /*Check that the database exists here: /data/data/your package/databases/Da Name
    private boolean checkDataBase()
    {
        File dbFile = new File(DB_PATH + DB_NAME);
        //Log.v("dbFile", dbFile + "   "+ dbFile.exists());
        return dbFile.exists();
    }
    */

    //Copy the database from assets
    private void copyDataBase() throws IOException
    {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        //Log.v("mPath", mPath);
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return mDataBase != null;
    }
    @Override
    public void close() 
    {
        if(mDataBase != null)
            mDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        try {
            // delete existing?

            // Copy the db from assests
            copyDataBase();
            Log.e(TAG, "database updated");
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString());
            try {
                throw mIOException;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
4

1 回答 1

1

有 2 种方法可以进行 onUpgrade,具体取决于您的应用。

1)删除所有旧表,然后运行 ​​onCreate。这基本上会清除所有旧数据并重新开始。如果您可以以某种方式重新生成旧数据,或者只是不关心它,那么它是一种很好的技术。

2)仔细维护每个发布版本之间架构的差异,并编写SQL语句以在它们之间进行适当的更改-添加新表,更改现有表以添加/删除列等。这既耗时又脆弱,因此请使用仅当您需要在这些版本之间保留数据时才这样做。

于 2013-04-17T20:14:53.083 回答