0

我通过从已经创建的sqlite数据库加载它来创建我的Android数据库,并将其从资产管理器复制到 android 创建的数据库中。

这是我的数据库助手类的代码。

public class DataBaseHelper extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";

    private static String DB_NAME = "myDBName";

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    /**
     * 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
     */
    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 does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        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 outputfile
        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) {

    }

}

我现在正在尝试在createDatabase()方法中调用该onCreate()方法。然后当我安装并运行我的应用程序并尝试通过调用打开数据库时openDatabase(),它会记录消息并向数据库实例变量抛出空指针异常,这意味着它无法打开它。但是当我createDatabase()从数据库助手类的构造函数或另一个类中调用时,它可以工作。请我不知道为什么会这样。

4

1 回答 1

0

没有看到堆栈跟踪,很难说......但有几件事要检查:

  1. 确保数据库有一个android_metadata带有 locale 字段的表(例如,我的值是“en_US”)。

  2. 当您的应用程序创建数据库时,rw-rw会自动为您/您的应用程序设置权限。建议您检查这些您要复制到/databases目录中的数据库。

  3. (最有可能的问题):与上面的第 2 点类似,检查文件/组所有权属性并确保将它们分配给您的应用程序。如果不是,您可以通过chown [your app's UID] : [your app's UID] /[PATH TO YOUR DATABASE FILE]在运行时使用Runtime.getRuntime().exec(commandLine) 方法执行此操作。

于 2013-09-23T03:25:48.520 回答