diff --git a/Tests/FMDatabaseTests.m b/Tests/FMDatabaseTests.m index 9c423dce..4776ff11 100644 --- a/Tests/FMDatabaseTests.m +++ b/Tests/FMDatabaseTests.m @@ -493,6 +493,17 @@ - (void)testUTF8Strings XCTAssertFalse([self.db hadError], @"Shouldn't have any errors"); } +- (void)testNonUTF8Data +{ + [self.db executeUpdate:@"create table utf8bug (a blob)"]; + XCTAssertTrue(([self.db executeUpdate:@"insert into utf8bug (a) values (?)", [NSData dataWithBytes:"\xFF\xFF" length:4]])); + + [self.db executeStatements:@"select * from utf8bug" withResultBlock:^int(NSDictionary *results) { + NSLog(@"%@", results); + return 0; + }]; +} + - (void)testArgumentsInArray { [self.db executeUpdate:@"create table testOneHundredTwelvePointTwo (a text, b integer)"]; diff --git a/src/fmdb/FMDatabase.m b/src/fmdb/FMDatabase.m index 3947241f..a42665b9 100644 --- a/src/fmdb/FMDatabase.m +++ b/src/fmdb/FMDatabase.m @@ -1210,6 +1210,9 @@ int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, for (NSInteger i = 0; i < columns; i++) { NSString *key = [NSString stringWithUTF8String:names[i]]; id value = values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null]; + if (value == nil) { + value = [NSString stringWithCString:values[i] encoding:NSASCIIStringEncoding]; + } [dictionary setObject:value forKey:key]; }