只读资产文件夹中的SQLite DB更新以获取新的应用程序版本

时间:2012-10-11 04:51:35

标签: android database sqlite assets

目前我正在摆弄自己在android中使用sql数据库并创建我的第一个应用程序。现在我想更新此应用程序和assets文件夹中的包含数据库。我用旧的数据库覆盖了旧数据库,但在我的手机上却没有显示新的数据库。

它以某种方式保存旧条目/查询或完全保存旧Db。我检查了网但无法真正找到解决方案。在我的dbhelper代码下面。我是否必须在onUpgrade中执行某些操作或执行什么操作?

非常感谢您的帮助!

public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH;

private static String DB_NAME;

private SQLiteDatabase db_object;

private final Context context;

public DataBaseHelper(Context context, String db) {
    super(context, db, null, 80);
    this.context = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
 //   DB_PATH = "/data/data/com.comp.appname/databases/";
    DB_NAME = "mydatabase.sqlite";
    try {
        createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
    InputStream myInput = context.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    db_object = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);
}

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

2 个答案:

答案 0 :(得分:2)

以下是升级数据库的代码:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {      
if (newVersion > oldVersion) {
    switch (oldVersion) {
        case 1:
            executeSQLScript(database, "update_v2.sql");
        case 2:
            executeSQLScript(database, "update_v3.sql");
    }
}
}

并查看此链接:http://www.drdobbs.com/database/using-sqlite-on-android/232900584?pgno=2

答案 1 :(得分:1)

我解决了自己的问题:

在您需要放置的onUpgrade方法中:

mycontext.deleteDatabase(DB_NAME);

在databasehelper类中我需要像这样增加版本号,例如:

super(context, db, null, 84); 

我的是84,但基本上你只需要增加1,说它是更高的版本。如果您的数据库版本正好随着数据库的每次外部编辑而增加,那么最佳情况就是。

希望能帮助他人。