如何从iOS中的Sqlite检索NSdata到NSdictionary

时间:2013-04-24 06:28:42

标签: ios sqlite nsdictionary nsdata

我正在创建使用Sqlite DB的iOS应用。 我创建了Table As:

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

然后我必须将self.selectedItemsDictnory Dictionary插入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);
        }

但我得到了超额例外。 请帮我检索数据

2 个答案:

答案 0 :(得分:1)

是否可以在此处粘贴JSON。 NSJSONSerialization的返回类型可以是依赖于根json对象的Dictionary或Array。

而不是

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];

答案 1 :(得分: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);
}