简体   繁体   中英

Error: incomplete SQL: ls

I am trying to access the database of another application and print it's contents to the console (USING ROOT), but for some reason I am getting this response:

E/[Error]: Error: incomplete SQL: ls
E/[Error]: exit

I don't really understand why this is happening. I tried to put my syntax in escaped quotations, I tried other sqlite commands, but it doesn't seem to work properly.

I can access the database and print it contents using ADB through my PC with the same path, and if I press the button in my application it asks me for root access, so those 2 are not the issues.

My dumbed down code:

onCreate

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnCon = (Button)findViewById(R.id.button1);
        btnCon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RunWithRoot("su shell -c sqlite3 \"data/data/app.package/databases/Database.db\" \"select * from messages;\"");
            }
        });
    }

RunWithRoot

private void RunWithRoot(String textView) {
        try {
            String line;
            Process process = Runtime.getRuntime().exec(textView);
            OutputStream stdin = process.getOutputStream();
            InputStream stderr = process.getErrorStream();
            InputStream stdout = process.getInputStream();

            stdin.write(("ls\n").getBytes());
            stdin.write("exit\n".getBytes());
            stdin.flush();

            stdin.close();
            BufferedReader br =
                    new BufferedReader(new InputStreamReader(stdout));
            while ((line = br.readLine()) != null) {
                Log.d("[Output]", line);
            }
            br.close();
            br =
                    new BufferedReader(new InputStreamReader(stderr));
            while ((line = br.readLine()) != null) {
                Log.e("[Error]", line);
            }
            br.close();

            process.waitFor();
            process.destroy();

        } catch (Exception ex) {
        }
    }

Does someone know what I'm doing wrong? I am very new to Android development, and even newer to root. If someone could throw me in the right direction with this, that would be MUCH appreciated.

PS: This app will be for personal use only, so I don't need checks to see if people have root, or if the path exists, etc.. I already heard a few developer's minds worrying heh

I fixed it using the following code.

private void RunWithRoot(){
    String line;
    Process process = Runtime.getRuntime().exec("su shell");
    OutputStream stdin = process.getOutputStream();
    InputStream stderr = process.getErrorStream();
    InputStream stdout = process.getInputStream();

    stdin.write("su -c 'sqlite3 \"/data/data/app.pkge/databases/Database.db\" \"select * from messages;\"'\n".getBytes());
    stdin.flush();

    stdin.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
    while ((line = br.readLine()) != null) {
        Log.d("[Output]", line);
    }
    br.close();
    br = new BufferedReader(new InputStreamReader(stderr));
    while ((line = br.readLine()) != null) {
        Log.e("[Error]", line);
    }
    br.close();

    process.waitFor();
    process.destroy();
}

And now it works correctly! Mostly thanks to @ScaryWombat!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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