0

我在下面有我的代码。它在我的资产文件夹中正确读取了我的 sqlite 数据库文件(我已经使用 SQLite 数据库浏览器创建了) - 将其移动到我设备上的 /data/data/packagename/databases/ 路径,然后我可以使用查询和光标来获取我的信息,效果很好。代码在这里:

public class DatabaseHelper extends SQLiteOpenHelper {

private Context myDbContext;
private static String dbName = "restaurant.db";
private static String outfilePath = "/data/data/dev.mypackage.com/databases/";
private static String path = outfilePath + dbName;
private static SQLiteDatabase db;

public DatabaseHelper(Context context){
    super(context, dbName, null, 2);
    this.myDbContext = context;
    db = openDb();
    String s = "select * from menu";
    Cursor c = db.rawQuery(s, null);
    Log.e("DB Constructor Row count", String.valueOf(c.getCount()).toString());

    while(c.moveToNext()){
        Log.e("DB Constructor", c.getString(c.getColumnIndex("category")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("menuItem_id")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("title")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("desc")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("price")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("icon")));
    }

    c.deactivate();
    c.close();
}

private void copyDataBase(File dbFile) throws IOException {
    try{
        InputStream dbStream = myDbContext.getAssets().open(dbName);
        OutputStream newDbFile = new FileOutputStream(outfilePath + dbName);

        byte[] buffer = new byte[1024];
        int length;
        while((length = dbStream.read(buffer)) > 0){
            newDbFile.write(buffer);
        }

        newDbFile.flush();
        newDbFile.close();
        dbStream.close();
    }
    catch(IOException e){

        throw new IOException("trying to copy the database - ERROR: " + e.getMessage());
    }   

}

private SQLiteDatabase openDb() throws SQLiteException{
    File dbFile = myDbContext.getDatabasePath(dbName);

    if(!dbFile.exists()){
        Log.e("openDb", "file does not exist");

        try {
            copyDataBase(dbFile);
        } catch (IOException e) {
            throw new RuntimeException("Error creating source database", e);
        }
    }

    return SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

}
public void loadRestaurantInfo(){

}

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

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

}

现在我不得不返回并使用 SQLite 浏览器向其中一个表(在资产文件夹中的数据库中)添加一行信息,但这并没有反映在我的光标输出中 - 我明白为什么会这样- 因为 if(!dbFile.exists()) 失败所以没有复制要做。所以我的问题是 - 我需要添加什么代码才能完成这项工作?我从来没有用代码创建表,所以我看不出 onUpgrade 方法对我有多大用处。

4

2 回答 2

1

做事的“正确”方式与您的出发方式完全不同。我不会去那里,而是假设您想保留当前创建数据库的方法,我会提供使用它的建议。向您的数据库添加一个表,该表具有单行元数据,其中将包括数据库版本(以及您喜欢的任何其他内容)。如果数据库文件已经存在,打开它并检查版本。如果版本较旧,请将其关闭并更换。

于 2012-03-11T20:59:44.017 回答
1

你有三个选择:

  1. 使用 onUpgrade() (首选)

  2. 删除现有数据库并复制新数据库(不是一个好主意)

  3. 复制现有数据库中的数据,删除现有数据库,复制新数据库,将旧数据库中的数据插入新数据库(在onUpgrade中可以升级新数据库架构时工作量太大)。

因此,要回答您的问题,您可以在 onUpgrade() 中升级数据库,而无需重新创建任何表。

另一方面,如果您只是向特定表添加了新行,则数据库架构没有更改,您可以在运行时插入新行......当然,不知道您的应用程序的目的是什么,这可能不是一个好主意,因为您很容易忘记对“静态”数据库的更改。

于 2012-03-11T21:05:56.443 回答