How to read the database of third-party applications? (PART 2)

In the previous article, we have obtained the database created by an application. How to view the data in the database? Of course, you can use some tools to view the db file. If I tell the manager so, will he be crazy? Insert picture description here
In this case, let's step to the stomach! Uh, one step! Since we can access the directory where the db is located, theoretically it is completely possible to manipulate the file, right? In Android, we can use SQLiteDatabase to operate the database. Stop it, the directory where db is currently in is private space.
Insert picture description here
Therefore, before we operate the database, we need to do a magical thing, that is, copy a db file to the public space. It's easy to say, but can we achieve it through IO streams? Obviously, it will end in failure due to permission issues. At this time, we still pin our hopes on the command line. We can copy files through the cat command (after ROOT of course), the code is as follows:

public static void move(String oldPath,String newPath){
        Process process = null;
        DataOutputStream os = null;
        try{
            String cmd = "cat "+oldPath+" >"+newPath;
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd+"\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        }catch (Exception e){
            Log.e("exception:",e.getMessage());
        }finally {
            try{
                if(os!=null){
                    os.close();
                }
                process.destroy();
            }catch (Exception ee){}
        }
    }

After copying the db file to the public space (such as external storage), we can operate this database through SQLiteDatabase. Then there is a small question, how do we know which tables are in the database? The reason why I say small problems, not small problems, is because it is too small haha ​​(if you understand the sqlite_master system table).
SQLiteDatabase will automatically generate a built-in table sqlite_master every time a new database (db) is created. Let's look at the structure of this table:

CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
); You

should be able to guess what this is for, right? This table stores information about all tables in the current database, such as the name of the table, the SQL statement used to create this table, the index, the table to which the index belongs, and the SQL statement used to create the index. So, we can get what tables are in db through this table.
Not much to say, the code:

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
        List<String> tables = new ArrayList<>();
        if(db!=null){
            Cursor cursor = db.rawQuery("select name from sqlite_master where type='table' order by name", null);
            while (cursor.moveToNext()) {
                String  name = cursor.getString(0);
                tables.add(name);
            }
        }

Up to now, all the tables in the database and the database have been fetched, and it is only a matter of printing the data in the table. At this point, the requirement is no longer any difficulty, directly on the code:

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
                List<String> datas = new ArrayList<>();
                if(db!=null){
                    Cursor cursor = db.rawQuery("select * from "+tbName, null);
                    while (cursor.moveToNext()) {
                        String data = "";
                        for(int i = 0;i<cursor.getColumnCount();i++){
                            data+=cursor.getColumnName(i)+":"+cursor.getString(i)+"\n\n";
                        }
                        datas.add(data);
                    }
                }

The data has been obtained. As for what to use, it is a personal question of the manager~

A great charm of programming is that it can turn the impossible into possible!

Guess you like

Origin blog.csdn.net/RobinTan24/article/details/108535253