1

我有名称的数据库:MyDb.sqlite

然后在我的数据库文件中有 2 个表

1 个用户表

row1 id_users (primary key,auto increment,int)

row2 username (varchar)

and value is empty

2 表 android_metabata

row1 locale (text) then values is en_US

我将我的数据库放入资产文件夹

JAVA类

public class DataBaseHelper extends SQLiteOpenHelper{

    private static String DB_PATH = "/data/data/com.example/databases/";

    private static String DB_NAME = "MyDb.db";

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    private static DataBaseHelper sInstance = null;


    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{

            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){

            //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;


        OutputStream myOutput = new FileOutputStream(outFileName);


        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{


        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }
    public Cursor select(String query) throws SQLException {
        return myDataBase.rawQuery(query, null);
    }


    public void insert(String table, ContentValues values) throws SQLException {
        myDataBase.insert(table, null, values);
    }


    public void delete(String table, String where) throws SQLException {

        myDataBase.delete(table, where, null);

    }


    public void update(String table, ContentValues values, String where) {

        myDataBase.update(table, values, where, null);

    }
    public void sqlCommand(String command) {
        myDataBase.execSQL(command);
    }

    @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) {

    }

    public static DataBaseHelper instance() {
        /*
        if (sInstance == null) {
            sInstance = new DataBaseManager();
        }
        */
        return sInstance;
    }

}

进程复制成功然后我使用本教程查看文件夹 data/data/my_package_name/databases

但是我的表用户无法复制并且我的表 android_metadata 复制成功,为什么?

谢谢。

4

2 回答 2

1

试试这个代码:

public DataBaseHelper(Context context)
    {
        super(context, DB_NAME, null, 1);
        this.mContext = context;
        DB_PATH="/data/data/" + context.getPackageName() + "/databases/";
    }   

    public void openDataBase() throws SQLException
    {
        Log.e("Open", "open Database");
        mDbHelper = new DataBaseHelper(mContext);
        mDataBase = mDbHelper.getWritableDatabase();
    }
    @Override
    public synchronized SQLiteDatabase getWritableDatabase() 
  {
      SQLiteDatabase db = null;

        if (!existsDataBase()) {
            try 
            {
                copyDataBase ();
                db = super.getWritableDatabase();

            } catch(Exception ex) 
            {
                ex.printStackTrace();
                Log.e("Database Log", DB_PATH + " failed to copy correctly. " + ex.getLocalizedMessage());
            }
        }
        else {
            db = super.getWritableDatabase();
        }

        return db;

  }

private void copyDataBase() throws IOException {

        Log.e("copyDataBase", " called");
        // Open your local db as the input stream
        InputStream myInput = mContext.getAssets().open(DB_NAME);
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
        mDataBase=mContext.openOrCreateDatabase(DB_PATH+DB_NAME, Context.MODE_PRIVATE, null );
        // 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();
        //

    }

// 像这样调用你的 DataBaseHelper 类: mDatabase= new DataBaseHelper (this); mDatabase.openDataBase(); //它将复制现有数据库,如果您需要添加更多表,请在 openDatabase 方法中执行此操作

于 2013-02-13T10:54:43.920 回答
0

只需将此行放入您的代码中: myContext.openOrCreateDatabase(DB_PATH+DB_NAME, Context.MODE_PRIVATE, null );

您更新后的 copyDataBase() 方法变为:

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;

    myDataBase=myContext.openOrCreateDatabase(DB_PATH+DB_NAME, Context.MODE_PRIVATE, null );

    OutputStream myOutput = new FileOutputStream(outFileName);


    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();

}
于 2013-02-12T12:47:30.657 回答