prom85
prom85

Reputation: 17868

open ANY SQLiteDatabase as su/root

I know, I can start a process as root user, but how can I create a SQliteDatabase with a given path (ANY path, i.e. another's app database)? As this root user in this root process...

I tried reading the file directly, but even this does not work (it asks EVERY TIME for root access again) and it only prints an empty result (because sqlite3 is 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 content of another app's database, how can I do that properly? I would be content with one of the two following solutions:

PARTIALLY SOLUTION FOUND:

see my answer...

BUT: still one problem, it asks for su permissions EVERY TIME, how can I avoid this?

Upvotes: 4

Views: 1158

Answers (2)

prom85
prom85

Reputation: 17868

Following works so far, only with one problem: it asks for permissions everytime... Probably because of the new process...

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
}

Upvotes: 1

tech.mohit.garg
tech.mohit.garg

Reputation: 690

first copy the database file in "assets" folder in your project and after the make a database file to connect with Database(sqlite database )

public class dbdata extends 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

}

make your own function like this

// Adding new contact relation table

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
}

}

Upvotes: 0

Related Questions