【问题标题】:SQLiteOpenHelper onCreate doesn't create database - or it is being overwritten immediatelySQLiteOpenHelper onCreate 不创建数据库 - 或者它被立即覆盖
【发布时间】:2011-07-19 00:13:40
【问题描述】:

我的数据库存储在我的资产文件夹中,并在运行时复制过来。我目前有一个简单的 Activity 进行简单的数据库调用:

    DBAdapter adapter = new DBAdapter(HomeActivity.this);
    final SQLiteDatabase db = adapter.getReadableDatabase();
    final Cursor c = db.query("exercises", new String[] { "name" }, null,
            null, null, null, null);
    startManagingCursor(c);
    if (c.moveToFirst())
        Log.e(TAG, c.getString(0));
    else
        Log.e(TAG,"No dice");

下面是我的 DBAdapter.java(它扩展了 open helper):

public class DBAdapter extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static String DB_PATH = "";
    private static final String DB_NAME = "gymrat.db";
    private final Context myContext;
    private static final String TAG = "GymRat.DBAdapter";

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/"
                + context.getApplicationContext().getPackageName()
                + "/databases/";
    }

    /**
     * Copies your 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 bytestream.
     * */
    private void copyDatabase() throws IOException {
        InputStream myInput = myContext.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();
    }

    /**
     * Call on creating data base for example for creating tables at run time
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e(TAG, "onCreate");
        try {
            copyDatabase();
        } catch (IOException e) {
            Log.e(TAG,"IOException copying Database");
        }
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Log.e(TAG, "onOpen");

        try {
            db.rawQuery("select * from exercises", null);
        } catch ( SQLiteException e) {
            Log.e(TAG,"DB copy didn't work");
            try {
                copyDatabase();
            } catch (IOException e1) {
                Log.e(TAG,"IOException recopying DB");
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e(TAG, "Upgrading");
    }
}

我从here 获取了这个适配器并对其进行了修改,因为它似乎没有使用 OpenHelper 功能。

我的问题:每当我的应用在安装后或擦除应用上的数据后第一次运行时,onCreate 会按照我的预期首先被调用,然后调用 copyDatabase 方法。问题是数据库没有被复制——或者如果复制了,它会立即被覆盖。而是创建了一个只有“android meta_data”表的默认数据库,导致我的活动中的查询抛出异常,因为该表不存在。

如果我在 onOpen 方法中再次复制数据库,它工作正常。我是在做一些乱七八糟的事情还是错过了一些导致 SQLiteOpenHelper 创建默认数据库的调用?创建的默认数据库使用构造函数中指定的相同名称和版本号。

现在,如您所见,我正在使用 onOpen 中的虚拟查询来查看预期的表是否存在,如果不继续并重新复制数据库。另一种选择是在调用 onCreate 时设置一个标志,发送信号 onOpen 以复制数据库。显然这些都有点hacky,我真的很好奇发生了什么。

我计划将实际的数据库调用移动到 Activity 之外的单独帮助程序类,我只是直接调用 db 来测试它。

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    如果您想在第一次运行时从资产中复制数据库并从 data/data/program.name/database 打开数据库,您应该使用如下代码:

    public class YourDatabase extends SQLiteOpenHelper
    
    public YourDatabase(Context context) {
        super(context, DatabaseName, null, DATABASE_VERSION);
        this.dbContext = context;
        // checking database and open it if exists        
        if (checkDataBase()) {
        openDataBase();
        } else {
        try {
                this.getReadableDatabase();
                copyDataBase();
            this.close();
            openDataBase();
    
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
         Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();
        }
    }
    

    您可以从this question 阅读完整答案

    【讨论】:

    • 谢谢,我最终做了这样的事情。我只是讨厌所有这些示例都使用 SQLiteOpenHelper 但不使用 onDatabaseCreate (或其他接口方法)来实现其隐含目的这一事实。我猜该方法只是假设您使用的是自动创建的数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多