0

我正在创建iOS使用 Sqlite DB 的应用程序。我创建了Table

const char *sql_stmt = "CREATE TABLE IF NOT EXISTS ORDERTABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMDESC TEXT)";

然后我必须将 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_open(dbpath, &orderDB) == SQLITE_OK)
    {            
        const char* sqlStatement = [[NSString stringWithFormat:@"SELECT * FROM ORDERTABLE WHERE (ID = '%d')",self.orderSelected]cStringUsingEncoding:NSUTF8StringEncoding];
        sqlite3_stmt* statement;

            if (sqlite3_prepare_v2(orderDB, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {


                if( sqlite3_step(statement) == SQLITE_ROW )
                {

               NSData *retData = (__bridge NSData*) sqlite3_column_value(statement, 1);
               NSDictionary *jsonObject=[NSJSONSerialization
                                              JSONObjectWithData:retData
                                              options:NSJSONReadingMutableLeaves
                                              error:nil];
                    NSLog(@"jsonObject is %@",jsonObject);
                    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfData:retData];
                    NSLog(@"dic is %@",dic);

                }
            }

            sqlite3_finalize(statement);
            sqlite3_close(orderDB);
        }

但我得到了严重的额外异常。请帮助我检索数据

4

2 回答 2

1

如果您可以在此处粘贴 JSON,是否可以。返回类型NSJSONSerialization可以是字典或数组,它取决于根 json 对象。

代替

NSDictionary *jsonObject=[NSJSONSerialization
                                          JSONObjectWithData:retData
                                          options:NSJSONReadingMutableLeaves
                                          error:nil];

尝试

NSArray *jsonObject=[NSJSONSerialization
                                          JSONObjectWithData:retData
                                          options:NSJSONReadingMutableLeaves
                                          error:nil];

还有一个好习惯是使用错误参数来注册任何异常,这样你就可以使用

NSError *logError = nil;
NSArray *jsonObject=[NSJSONSerialization
                                          JSONObjectWithData:retData
                                          options:NSJSONReadingMutableLeaves
                                          error:&error];
于 2013-04-24T06:45:23.373 回答
0

尝试以下代码从 sqlite 获取数据

if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
 {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        int uniqueId = sqlite3_column_int(statement, 0);
        char * temp = (char *) sqlite3_column_text(statement, 1);  
        NSString *item_desc = [[NSString alloc] initWithUTF8String:temp];                                  
    }
    sqlite3_finalize(statement);
}
于 2013-04-24T07:03:14.547 回答