SQLite blob数据验证/测试

时间:2016-01-12 16:59:15

标签: android sqlite testing blob

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

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

将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并将其从光标中取出来?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

cursorRowToContentValues()并不一定有用。

Cursor的{​​{3}}表示只有当AbstractWindowedCursorequals()的实例时,才会返回Blob。

即使它确实将一个字节数组数据从Blob实例返回到ContentValues,ContentValues中的entry.getValue().getClass().equals(byte[].class)也会显示两个不相等的字节数组。

我建议您知道哪些列是BLOB并单独验证它们。或者做这样的事情: Array.equals((byte[]) entry.getValue(), cursor.getBlob)检查create function dbo.getNumOfReviews2 (@email varchar(40)) returns int as begin declare @numOfReviews int select @numOfReviews = count(*) from dbo.Reviews where email = @email group by Email return @numOfReviews end CREATE TRIGGER setDiscount ON dbo.[Contains] FOR INSERT AS DECLARE @OrderID int DECLARE @ProductID int DECLARE @Size VarChar(15) DECLARE @temp int IF CURSOR_STATUS('global','C_CURSOR')>=-1 BEGIN DEALLOCATE C_CURSOR END DECLARE C_CURSOR CURSOR FOR SELECT ProductID,OrderID,Size FROM INSERTED BEGIN OPEN C_CURSOR FETCH NEXT FROM C_CURSOR INTO @ProductID,@OrderID,@Size WHILE (@@FETCH_STATUS=0) BEGIN @temp = dbo.getNumOfReviews2(select BillingEmail from dbo.Orders where OrderID=@OrderID) IF (SELECT COUNT(*) FROM dbo.[Contains] WHERE OrderID = @OrderID) > 5 or (SELECT sum(Quantity) FROM dbo.[Contains] WHERE OrderID=@OrderID) > 10 or ( @temp )> 5 UPDATE [Contains] SET [Savings%] = [Savings%] + 0.05 WHERE OrderID = @OrderID and ProductID = @ProductID and Size = @Size FETCH NEXT FROM C_CURSOR INTO @ProductID,@OrderID,@Size END END