SQLiteDiskIOException:磁盘I/O错误(代码3850)

pro*_*m85 5 sqlite android android-sqlite

我在某些设备上遇到上述错误(很少见,直到现在才2次):

android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 3850)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForString(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:679)
    at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:361)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:236)
    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:200)
    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
    at ***.a(***.java:115)
Run Code Online (Sandbox Code Playgroud)

我的代码中的第115行如下:

// here the exception occurs
SQLiteDatabase db = SQLiteDatabase.openDatabase(pathApp, null, SQLiteDatabase.OPEN_READONLY); 
// ...
db.close();
Run Code Online (Sandbox Code Playgroud)

故事

我的工作如下:

  • 应用程序具有root权限
  • 它将数据库从另一个应用程序复制到它自己的目录中
  • 它试图打开这个数据库并读取一些数据
  • 它再次关闭文件

就这样.这适用于数千种设备.我的应用程序肯定只能在现场访问数据库,我完全确定

有谁知道什么可能导致这个问题?

也许有趣的事实

  • 这2个设备是OnePlus2
  • 一个人告诉我,更新到氧气2.1后出现问题

pro*_*m85 0

这是有效的解决方案(已经在生产中测试了 2 个月左右),它也适用于 OnePlus2 和 Android 6:

然后在您的应用程序中,尝试读取外部数据库,如下所示:

  • 找出哪个二进制文件正在运行。我尝试了几种方法(/proc/cpuinfo例如通过调用),但找不到可靠的解决方案,所以我通过反复试验来完成,如下所示:
    • 首先,我将二进制文件从资产文件夹复制到应用程序files文件夹
    • 然后我尝试通过转储所需的数据库<path_to_SQLite3_bnary> <path_to_database> \.dump '<tablename>'\n并读取结果
    • 我检查结果,如果它包含error:我知道二进制文件不起作用,如果它开头INSERT INTO我知道它有效
    • 我对我的二进制文件重复此操作,直到找到可用的二进制文件
  • 然后我就读了结果。结果将包含错误或者是一些 csv 类似的内容。我要么处理错误,要么通过将每一行转换为有效的 csv row.replace("INSERT INTO \"" + tablename + "\" VALUES(", "").replace(");", ""),以便我以某种格式获取内容csv。我使用 opencsvCSVReader来解析数据然后......

就是这样,这在所有设备上都可以正常工作(直到知道),没有问题

代码 - 从我的来源复制,稍微采用它来满足您的需求

public static List<String> readCSVData(String pathDatabase, String tableName) throws InterruptedException, TimeoutException, RootDeniedException, IOException 
{
    List<String> res = null;
    List<String> csvData = null;

    final String[] architectures = new String[]{
            "armv7",
            "armv7-pie",
            "armv6",
            "armv6-nofpu"
    };

    for (int i = 0; i < architectures.length; i++)
    {
        res = readDatabase(architectures[i], pathDatabase, tableName, rootMethod);

        if (res.toString().contains("[error:"))
        {
            L.d(RootNetworkUtil.class, "Trial and Error - ERROR: " + architectures[i]);
        }
        else
        {
            int maxLength = (res.toString().length() < 100) ? res.toString().length() : 100;
            L.d(RootNetworkUtil.class, "Trial and Error - RESULT: " + res.toString().substring(0, maxLength));
            L.d(RootNetworkUtil.class, "Architecture found via trial and error: " + architectures[i]);
            csvData = res;
            break;
        }
    }

    return csvData;
}

private static List<String> readDatabase(String architecture, String pathDB, String tablename) throws InterruptedException, TimeoutException, RootDeniedException, IOException {
    String sqlite = "sqlite3." + architecture;
    String pathSQLite3 = getSQLitePath(architecture, tablename);
    // OWN class, just copy the file from the assets to the sqlite3 path!!! 
    AssetUtil.copyAsset(sqlite, pathSQLite3);

    String[] cmd = new String[]{
            "su\n",
            //"chown root.root " + pathSQLite3 + "\n",
            "chmod 777 " + pathSQLite3 + "\n",
            pathSQLite3 + " " + pathDB + " \".dump '" + tablename + "'\"\n"
    };
    List<String> res = new ArrayList<>();
    List<String> temp = RootUtils.execute(cmd);
    for (int i = 0; i < temp.size(); i++)
    {
        // Fehlerzeilen behalten!!!
        if (temp.get(i).contains("error:"))
            res.add(temp.get(i));
        else if (temp.get(i).startsWith("INSERT INTO \"" + tablename + "\""))
            res.add(temp.get(i).replace("INSERT INTO \"" + tablename + "\" VALUES(", "").replace(");", ""));
    }
    return res;
}

public static String getSQLitePath(String architecture, String addon)
{
    String sqlite = "sqlite3." + architecture;
    String pathSQLite3 = "/data/data/" + MainApp.get().getPackageName() + "/files/" + sqlite + addon;
    return pathSQLite3;
}
Run Code Online (Sandbox Code Playgroud)