繁体   English   中英

SQLite文件能否成功复制到未经授权的Android设备的数据文件夹中?

[英]Can SQLite file copied successfully on the data folder of an unrooted android device ?

我知道,为了访问设备上的数据文件夹,需要植根。 但是,如果我只想将数据库从我的资源文件夹复制到设备上的数据文件夹,那么复制过程是否可以在无根电话上运行? 以下是我的数据库助手类。 从logcat,我可以验证是否成功返回了对copyDataBase(),createDataBase()和openDataBase()的方法调用。 但是,我收到此错误消息

android.database.sqlite.SQLiteException:没有这样的表:TABLE_NAME:

当我的应用程序执行rawQuery时。 我怀疑数据库文件没有成功复制(不能太确定,因为我没有访问数据文件夹),但是对copyDatabase()的方法调用不会引发任何异常。 会是什么呢? 谢谢。

ps:我的设备仍然没有根据,我希望它不是错误的主要原因。

public DatabaseHelper(Context context) {

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

 public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        String s = new Boolean(dbExist).toString();
        Log.d("dbExist", s );

        if(dbExist){
            //do nothing - database already exist
            Log.d("createdatabase","DB exists so do nothing");
        }else{


        this.getReadableDatabase();

            try {

                copyDataBase();
                Log.d("copydatabase","Successful return frm method call!");

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }


    private boolean checkDataBase(){

        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
            }


    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = null;


            myInput = myContext.getAssets().open(DB_NAME);


            Log.d("copydatabase","InputStream successful!");


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

    }*/

    public void close() {
        // NOTE: openHelper must now be a member of CallDataHelper;
        // you currently have it as a local in your constructor
        if (myDataBase != null) {
            myDataBase.close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }



}

我假设您的DB_NAME与您的assests文件夹中存在的数据库不同。

如果您正在关注reigndesign ,请检查以下行:

private static String DB_NAME ="myDBName";  

这里的DB_NAME是您的数据库的名称。 假设您在assets文件夹中有数据库的副本。 您的DB_NAME必须与assets文件夹中的相同。

要么

按照这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM