简体   繁体   English

无需生根即可访问数据库

[英]Access database without rooting

I've already vainly searched extensively to find a method to access db without rooting my android phone. 我已经徒劳地进行了广泛搜索,以找到一种无需扎根Android手机即可访问db的方法。 I don't want to use AVD as it's very slow. 我不想使用AVD,因为它的速度非常慢。 But in order to see what I'm doing I need to access my app .db file from PC or phone in someway. 但是,为了查看我在做什么,我需要以某种方式从PC或电话访问我的app .db文件。

/data/data//databases/.db doesn't work for real devices. /data/data//databases/.db在实际设备上不起作用。

So, please suggest me some method by which I can see my database. 因此,请建议我一些可以查看数据库的方法。

Create a function in your app to copy the DB from the internal (protected) memory to external (unprotected) such as an SD card. 在您的应用程序中创建一个函数,以将数据库从内部(受保护的)内存复制到外部(不受保护的)SD卡等。 Then you can access it with your PC (or even an Android DB reader from the device itself if you want to). 然后,您可以使用PC(如果需要,甚至可以从设备本身使用Android DB阅读器)访问它。

Just add code in your application on specific event it will copy the DB into SD card, it will be copy of ur DB/data not actual DB. 只需在特定事件中在您的应用程序中添加代码,它将数据库复制到SD卡中,它将复制您的数据库/数据而不是实际的数据库。 From SD card you can always access the DB. 通过SD卡,您可以随时访问数据库。 this is the work around but it works for me. 这是可以解决的,但对我有用。

Here is the code 这是代码

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
            String currentDBPath = "data/"+sPackageName+"/databases/"+sDBName;
            String backupDBPath = "/.appname-external-data-cache/"+sDBName; //"{database name}";
            File dir = new File(sd,backupDBPath.replace(sDBName,""));
            if(dir.mkdir()) {

            }
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);
            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        } 

   } catch (Exception e) {

    }

Here is the code I think you are after: 这是我认为您追求的代码:

try {
                // Backup of database to SDCard
                db.open();
                File newFile = new File(Environment.getExternalStorageDirectory(), "BackupFileNameHere");
                InputStream input = new FileInputStream("/data/data/com.YourProjectName/databases/DatabaseNameHere");
                OutputStream output = new FileOutputStream(newFile);

                // transfer bytes from the Input File to the Output File
                byte[] buffer = new byte[1024];
                int length;
                while ((length = input.read(buffer))>0) {
                    output.write(buffer, 0, length);
                }
                output.flush();
                output.close();
                input.close();
                db.close();

            } catch (Exception e) {
            }

I hope this helps. 我希望这有帮助。

We just set the file permissions to readable for all users from within the app. 我们只是将文件权限设置为对应用程序中的所有用户可读。

if (BuildConfig.DEBUG)
{
     new File(mDB.getPath()).setReadable(true, false);
}

Then just get the .db with adb -pull 然后只需使用adb -pull获取.db

adb -d pull //data/data/xxxxx/databases/xxxxx.db .

NOTE: I've discovered that this needs to be done each time the database file is opened, for example in onCreate as well as the constructor of your SQLiteOpenHelper wrapper (when your database is not null) or perhaps onOpen. 注意:我发现每次打开数据库文件时都需要执行此操作,例如在onCreate以及SQLiteOpenHelper包装器的构造函数(当数据库不为null时)或onOpen中。 If only in onCreate, then the next time you run your app and the .db already exists, for some reason the permissions have been changed back. 如果仅在onCreate中,则下次运行应用程序且.db已存在时,由于某种原因,权限已更改回。 This might have something to do with how Android manages its data. 这可能与Android如何管理其数据有关。

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

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