【问题标题】:SQLite blob data validation / testingSQLite blob 数据验证/测试
【发布时间】:2016-01-16 10:12:04
【问题描述】:

我正在为具有 blob 数据类型的 SQLite 数据库创建测试。我想验证 blob(字节数组 - 图片)。

这就是我所拥有的,现在我没有检查导致错误的 PICTURES_COLUMN(无法将 blob 解析为字符串)。

比较由 ContentValues 制成的 Set 与光标:

  public void testHotelSingleInsert() {
        SQLiteDatabase testDb = testDbHelper.getWritableDatabase();
        long testRowId;

        ContentValues testContentValues = TestUtils.getSingleContentValue(testContext);
        testRowId = testDb.insert(HotelsEntry.TABLE_NAME, null, testContentValues);

        assertTrue("Error: single insert to hotel table failed", testRowId != -1L);

        Set<Map.Entry<String, Object>> testSet = testContentValues.valueSet();
        Cursor cursor = testDb.query(HotelsEntry.TABLE_NAME, null, null, null, null, null, null);
        cursor.moveToFirst();

        for (Map.Entry<String, Object> entry : testSet) {
            String columnName = entry.getKey();
            int columnId = cursor.getColumnIndex(columnName);

            if (entry.getKey().toString() != HotelsEntry.PICTURES_COLUMN) {
                assertEquals(
                        "Value: " + entry.getValue().toString() + " is not equal to: " + cursor.getString(columnId),
                        entry.getValue().toString(),
                        cursor.getString(columnId));
            }
        }
    }

如何以专业的方式验证该 blob?我是否有单独处理 blob 并将其从光标中取出?

【问题讨论】:

    标签: android sqlite testing blob


    【解决方案1】:

    要获取一行中的所有值,请使用cursorRowToContentValues()。 要比较两个 ContentValues 对象,请使用 equals()

    【讨论】:

    • 这不一定会转换 ContentValues 结果中的返回 Blob 类型,可能会导致触发:android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
    • @MichaelLee SQLiteCursor 实际上源自AbstractWindowedCursor
    【解决方案2】:

    cursorRowToContentValues() 不一定有效。

    cursorRowToContentValues()source code 表明,只有当 CursorAbstractWindowedCursor 的实例时,才会返回 Blob。

    即使它确实将字节数组数据从 Blob 实例返回到 ContentValues,ContentValues 中的equals() 也会显示两个字节数组不相等。

    我建议要么知道哪些列是 BLOB,然后单独验证它们。或者做这样的事情: entry.getValue().getClass().equals(byte[].class)查看Array.equals((byte[]) entry.getValue(), cursor.getBlob)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2018-06-10
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多