【问题标题】:Error: incomplete SQL: ls错误:不完整的 SQL:ls
【发布时间】:2016-09-25 07:48:38
【问题描述】:

我正在尝试访问另一个应用程序的数据库并将其内容打印到控制台(使用 ROOT),但由于某种原因,我收到了以下响应:

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

我真的不明白为什么会这样。我试图将我的语法放在转义引号中,我尝试了其他 sqlite 命令,但它似乎无法正常工作。

我可以通过我的 PC 以相同的路径访问数据库并使用 ADB 打印它的内容,如果我按下应用程序中的按钮,它会要求我进行 root 访问,所以这两个不是问题。

我的简化代码:

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) {
        }
    }

有人知道我做错了什么吗?我对 Android 开发非常陌生,甚至对 root 也很陌生。如果有人能把我引向正确的方向,那将不胜感激。

PS:这个应用程序仅供个人使用,所以我不需要检查人们是否有root,或者路径是否存在等等。我已经听到一些开发者的想法,呵呵

【问题讨论】:

  • 你为什么要stdin.write(("ls\n").getBytes());
  • @ScaryWombat 天哪,我真的很瞎。谢谢!但是,它说:“无法打开数据库“data/data/app.pkg/databases/Database.db”:无法打开数据库文件”现在
  • 路径是否正确?
  • 顺便说一句,不要沉默吞下异常`catch (Exception ex) {}`
  • @ScaryWombat 感谢您的提示!我将把它放在 try{}catch(Exception){System.out.print(ex)} 块中。路径是正确的 100% 肯定。我们可以把这个带到聊天室吗?当然,如果你有时间的话。

标签: java android sqlite shell root


【解决方案1】:

我使用以下代码修复了它。

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();
}

现在它可以正常工作了!主要感谢@ScaryWombat!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多