0

I'm trying to access a database using root and sqlite3 to get data from it.

So far I managed to display data in the logcat but I can't get it in a file or any other way.

Using RootCommand library this is my code :

public void readDB(String message){
    try {
        Shell shell = Shell.startRootShell();
        String setOutput= "sqlite3 .output /sdcard/number.txt"; // This didn't work because it treats .output as a database name
        String readTest = "sqlite3  /data/data/com.package/databases/someDB.db \"SELECT * FROM table\";";

        shell.add(new SimpleCommand(setOutput)).waitForFinish();
        shell.add(new SimpleCommand(readTest)).waitForFinish();
        Log.d(TAG , "finished");
    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
        Log.e(TAG, e.getMessage());
    }
}

As explained here I used .output to specify a file but it didn't work because it treats .output as a database name and throws an exception that it can't find the db and continues to display results in logcat.

And I tried writefile('/sdcard/myfile.txt',column) but no file and no data in logcat.

When I try .output /sdcard/myfile.txt from adb it works fine and I find the results in the file but I can't get it done through the code

So how to get this data in anyway? Is there any better approach to do this?

PS: I'm not accessing my own app data so I can't use sqlite database helper class.

Mohammad
  • 1,185
  • 1
  • 14
  • 26

1 Answers1

0

I would suggest this:

  1. Do really mean file of database is /data/data/com.package/databases/someDB.db ??? all android devices store their databases is /data/data/com.package/databases/someDB

  2. Have you tried full copy file from app /data/data/com.package/databases/someDB into /sdcard/myolddb_in_newplace ?

  3. Have you tried to use pipes ? like here https://unix.stackexchange.com/a/111610/125332

Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • 1. Yes this is file of database 2. Copying database is not an option because it's very big and changes very fast and I'll query it every time it changes. 3. Solved the problem. Thank you – Mohammad Mar 18 '17 at 21:33
  • Is there anyway to get the result as a String variable? – Mohammad Mar 18 '17 at 21:55
  • What do you mean? read the url I wrote. You can output the command line log both String and file – Vyacheslav Mar 18 '17 at 21:57
  • I mean like `String result = theQueryResult` instead of writing it into a file and the link doesn't show this approach. Thank you. – Mohammad Mar 18 '17 at 22:24
  • Ok. You can use `Process` class to read stdout: http://stackoverflow.com/a/5711150/1979882 – Vyacheslav Mar 18 '17 at 22:29