Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSCOCAERRORDOMIN代码=3840将NSDATA检索到NSDictionary时出现问题_Ios_Sqlite_Nsdata_Nsjsonserialization - Fatal编程技术网

Ios NSCOCAERRORDOMIN代码=3840将NSDATA检索到NSDictionary时出现问题

Ios NSCOCAERRORDOMIN代码=3840将NSDATA检索到NSDictionary时出现问题,ios,sqlite,nsdata,nsjsonserialization,Ios,Sqlite,Nsdata,Nsjsonserialization,我正在创建使用SQLite DB的iOS应用程序。 我已将表创建为: const char *sql_stmt = "CREATE TABLE IF NOT EXISTS ORDERTABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMDESC BLOB)"; 然后我必须将self.selectedItemsDictnory字典插入ItemDESC 我插入如下内容: NSData *data = [NSJSONSerialization dataWi

我正在创建使用SQLite DB的iOS应用程序。 我已将表创建为:

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);
}
我得到了错误的答案

错误域=NSCOCAERRORDOMAIN Code=3840“无法执行该操作 已完成。(Cocoa错误3840。)“(JSON文本不是以数组或 对象和允许不设置片段的选项。)


您没有格式化JSON属性。在这方面:

NSData *data = [NSJSONSerialization dataWithJSONObject:self.selectedItemsDictnory options:NSJSONWritingPrettyPrinted error:nil];
现在您有了一个数据blob,而不是字符串,但在下面,您将其视为字符串:

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
NSString *jsonString = [NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",jsonString];
如果需要字符串:

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",data];
NSString *jsonString = [NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC)VALUES( \"%@\");",jsonString];

我插入的NSDictionary就是一个例子:po self.selectedItemsDictnory$1=0x07184170{Light=50;面食=220;}我试图搜索这个错误。我发现我正在检索的JSON实际上不是JSON格式。请提供您的意见,谢谢