Open ANY SQLiteDatabase as su / root

I know that I can start the process as the root user, but how can I create an SQliteDatabase with the given path (ANY path, i.e. another application database)? How is this root user in this root process ...

I tried reading the file directly, but even this does not work (it again asks EVERY TIME for root access) and it prints only an empty result (because sqlite3 was not found)

 String path = "/data/data/<app_package>/databases/<database_name>";
 Process p = Runtime.getRuntime().exec("su sqlite3 -csv " + path + " \"select * from test\";");
 StringBuilder res = new StringBuilder();
 String line;
 while ((line = bufferedReader.readLine()) != null)
     res.append(line + "\n");

Actually, I want to read the contents of another application database, how can I do this correctly? I would be pleased with one of the following two solutions:

  • get request contents like csv or something like that

  • gets the object SQLiteDatabaseas root, with the given access path to the database (preferable since it is much more flexible)

PARTIAL SOLUTION:

. ...

: , su , ?

+4
2

: ... , - ...

try
{
String dbName = "<database_name>";
String packageSrc = "<package_source>";
String pathSource = "/data/data/" + packageSrc + "/databases/" + dbName;
String pathTarget = "/data/data/" + MainApp.getAppContext().getPackageName() + "/databases/" + dbName;

// 1) delete old copied file
// 2) copy file
// 3/4) set permissions of copied file
String commandDelete = "rm " + pathTarget + "\n";
String commandCopy = "cat " + pathSource + " > " + pathTarget + "\n";
String commandCHOwn = "chown root.root " + pathTarget + "\n";
String commandCHMod = "chmod 777 " + pathTarget + "\n";

Process p = Runtime.getRuntime().exec("su");

OutputStream os = p.getOutputStream();

os.write((commandDelete).getBytes("ASCII"));
os.flush();
os.write((commandCopy).getBytes("ASCII"));
os.flush();
os.write((commandCHOwn).getBytes("ASCII"));
os.flush();
os.write((commandCHMod).getBytes("ASCII"));
os.flush();

os.close();

try
{
    p.waitFor();
    if (p.exitValue() != 255)
    {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(pathTarget, null, SQLiteDatabase.OPEN_READONLY);
        // be happy and work with the database
    }
}
catch (InterruptedException e)
{
    // error
}
}
catch (IOException e)
{
// error
}
+1

"assets" ( sqlite)

dbdata SQLiteOpenHelper {

private static String DBName = "dbdata";
private String DBVersion = "3";
private static Context context;
private static String DB_PATH = "";
private SQLiteDatabase myDataBase;
Context myContext;

public dbdata(Context context) {
    super(context, DBName, null, 1);

    myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    try {
        createDataBase();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

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

    // Path to the just created empty db
    String outFileName = DB_PATH + DBName;

    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 + DBName;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-gene rated method stub

}

//

public void addContact(personprp prop) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put("rl_first_name", prop.firstname);
    values.put("rl_last_name", prop.lastname);
    values.put("rl_age", prop.age);
    values.put("rl_dob", prop.dob);
    values.put("rl_address", prop.address);
    values.put("rl_category", prop.category);
    values.put("rl_firstcall", prop.firstcall);
    values.put("rl_firstmsg", prop.firstmsg);
    values.put("rl_brother", prop.brothers);
    values.put("rl_sister", prop.sisters);
    values.put("rl_mobile", prop.mobileno);
    values.put("rl_emailid", prop.emailid);
    values.put("rl_facebookid", prop.facebookid);
    values.put("rl_imagepath", prop.imagepath);

    db.insert("tb_contact", null, values);
    System.out.println("datasaved");

    // Inserting Row

    db.close(); // Closing database connection
}

}

0

Source: https://habr.com/ru/post/1542907/