NSCocoaErrorDomain代码= 3840问题,同时检索到NSDictionary的NSDATA
我正在创build使用SQLite DB的iOS应用程序。 我创build了表格为:
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS ORDERTABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMDESC BLOB)";
然后我必须插入self.selectedItemsDictnory字典到ItemDESC我插入为:
NSData *data = [NSJSONSerialization dataWithJSONObject:self.selectedItemsDictnory options:NSJSONWritingPrettyPrinted error:nil]; NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
取而代之,这是成功完成的。
现在我必须以相同的格式检索这本字典
我正在做的是:
if (sqlite3_prepare_v2(orderDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { int uniqueId = sqlite3_column_int(statement, 0); const void *blob = sqlite3_column_blob(statement, 1); NSInteger bytes = sqlite3_column_bytes(statement, 1); NSData *data = [NSData dataWithBytes:blob length:bytes]; NSError *error; NSMutableString *jsonObject=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"jsonObject is %@ with error %@",jsonObject, error); } sqlite3_finalize(statement); sqlite3_close(orderDB); }
而我越来越错误
错误域= NSCocoaErrorDomain代码= 3840“该操作无法完成。(cocoa错误3840.)”(JSON文本没有开始与数组或对象和选项,以允许片段没有设置。
您没有格式化JSON属性。 在这里:
NSData *data = [NSJSONSerialization dataWithJSONObject:self.selectedItemsDictnory options:NSJSONWritingPrettyPrinted error:nil];
现在你有一个数据块,而不是一个string,但在下面,你把它当作一个string:
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
如果你想要一个string:
NSString *jsonString = [NSString alloc] initWithData:data encoding: NSUTF8StringEncoding]; NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",jsonString];