简体   繁体   English

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

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

I know that in order to access the data folder on the device, it needs to be rooted. 我知道,为了访问设备上的数据文件夹,需要植根。 However, if I just want to copy the database from my assets folder to the data folder on my device, will the copying process works on an unrooted phone? 但是,如果我只想将数据库从我的资源文件夹复制到设备上的数据文件夹,那么复制过程是否可以在无根电话上运行? The following is my Database Helper class. 以下是我的数据库助手类。 From logcat, I can verify that the methods call to copyDataBase(), createDataBase() and openDataBase() are returned successfully. 从logcat,我可以验证是否成功返回了对copyDataBase(),createDataBase()和openDataBase()的方法调用。 However, I got this error message 但是,我收到此错误消息

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

when my application is executing rawQuery. 当我的应用程序执行rawQuery时。 I'm suspecting the database file is not copied successfully (cannot be too sure as I do not have access to data folder), yet the method call to copyDatabase() are not throwing any exception. 我怀疑数据库文件没有成功复制(不能太确定,因为我没有访问数据文件夹),但是对copyDatabase()的方法调用不会引发任何异常。 What could it be? 会是什么呢? Thanks. 谢谢。

ps: My device is still unrooted, I hope it is not the main cause of the error. 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) {

    }



}

I assume that your DB_NAME is not the same to the Database which exists in your assests folder. 我假设您的DB_NAME与您的assests文件夹中存在的数据库不同。

If you are following reigndesign , then check this line: 如果您正在关注reigndesign ,请检查以下行:

private static String DB_NAME ="myDBName";  

DB_NAME here is the name of your database. 这里的DB_NAME是您的数据库的名称。 It is assumed that you have a copy of the database in the assets folder. 假设您在assets文件夹中有数据库的副本。 Your DB_NAME must be the same which exists in the assets folder. 您的DB_NAME必须与assets文件夹中的相同。

OR 要么

Follow this . 按照这个

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

相关问题 如何从素材文件夹中读取要复制到Android模拟器或设备中的大型Sqlite文件? - How to read LARGE Sqlite file to be copied into Android emulator, or device from assets folder? / system / app中的Android应用程序可以在无根设备上写入/ system吗? - Can an Android app in /system/app write into /system on unrooted device? 从无根设备中的assets文件夹复制数据库 - Copy Database from assets folder in unrooted device 无法从资产文件夹Android复制SQLite数据库 - Sqlite database not copied from asset folder Android 在未经验证的Android设备上运行本机C程序 - Run a native C program on unrooted Android device 是否可以在无根的普通android设备上启用checkjni? - Is it possible to enable checkjni on unrooted normal android device? RtAudio软件可以在无根用户情况下录制Android OS设备的输出音频吗? - Can a RtAudio software record output audio of an Android OS Device, in an unrooted user case? 无法打开Assets文件夹中的sqlite文件复制到设备 - can't open sqlite file in assets folder to copy to device 在android设备的/ data文件夹中创建和写入文件 - Creating and writing file in to /data folder of android device Android-将文件保存到设备的“数据”文件夹 - Android - save file to Device's “data” folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM