问题描述
我正在创建使用 SQLite DB 的 iOS 应用程序.我已经创建了表为:
I am creating iOS app which uses SQLite DB.I have created Table As:
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS ORDERTABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMDESC BLOB)";
然后我必须将 self.selectedItemsDictnory Dictionary 插入到 ItemDESC我插入为:
and then I have to insert self.selectedItemsDictnory Dictionary into ItemDESCi am inserting as :
NSData *data = [NSJSONSerialization dataWithJSONObject:self.selectedItemsDictnory options:NSJSONWritingPrettyPrinted error:nil];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
到此就成功了.
现在我必须以相同的格式检索这本字典
Now i have to retrieve this dictionary in same format
我正在做的是:
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);
}
我收到错误
Error Domain=NSCocoaErrorDomain Code=3840 "操作不能完全的.(Cocoa 错误 3840.)"(JSON 文本未以数组或允许未设置片段的对象和选项.)
推荐答案
您没有设置 JSON 属性的格式.在此:
You are not formatting the JSON property. In this:
NSData *data = [NSJSONSerialization dataWithJSONObject:self.selectedItemsDictnory options:NSJSONWritingPrettyPrinted error:nil];
现在你有一个数据 blob,不是一个字符串,但在下面,你把它当作一个字符串:
Now you have a data blob, not a string, but below, you treat it as a string:
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
如果你想要一个字符串:
If you want a string:
NSString *jsonString = [NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",jsonString];
这篇关于将 NSDATA 检索到 NSDictionary 时出现 NSCocoaErrorDomain Code=3840 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!