2013-04-24 65 views
0

我正在创建使用Sqlite DB的iOS应用程序。 我已经创建Table由于:如何从iOS中的Sqlite中将NSdata检索到NSdictionary中

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); 
     } 

但我越来越坏过剩例外。 请帮我出来检索数据

+0

你能指出确切的线路,你在哪里得到异常? – 2013-04-24 06:34:03

+0

嗨,我收到异常在 NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:retData options:NSJSONReadingMutableLeaves error:nil]; – Sulabh 2013-04-24 06:36:40

+0

是retData是否为零? – karthik 2013-04-24 06:40:14

回答

1

如果你可以在这里粘贴JSON,这是可能的。返回类型NSJSONSerialization可能是一个Dictionary或一个数组,它依赖于根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]; 
+0

居然有键/值对 像{ 面食= 220; “春卷” = 125;} 它是一个约束服务员应用程序 其中,虽然接受订单,我在字典中保存物品的价格,然后插入sqlite订单表与nsdata – Sulabh 2013-04-24 06:51:46

+0

我想你应该通过本教程,希望它可以帮助http://www.raywenderlich。COM/5492 /工作与 - JSON-在-IOS-5 – 2013-04-24 06:59:17

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); 
} 
+0

喜KARTHIK 我得到导致ITEM_DESC 为PO ITEM_DESC $ 1 = 0x0715c6f0 <7b0a2020 224d4f4d 4f277322 203a2022 31323522 2c0a2020 22537072 696e6720 526f6c6c 7322203a 20223132 35220a7d> 所以我将如何将其转换为我的实际字典? – Sulabh 2013-04-24 07:13:30

+0

当你需要将nsdata插入ITEMDESC列时,将ITEMDESC的类型声明为BLOB,请参考http://stackoverflow.com/questions/4724118/retrieve-nsdata-from-sqlite-stored-in-blob-data-type - 对于iphone应用程序,http://stackoverflow.com/questions/4215505/sqlite3-insert-and-read-blob-data-in-database – karthik 2013-04-24 09:56:45

+0

嗨Karthik,我已经声明数据类型ITEM_DESC blob, 现在如何将字符串item_desc再次转换为nsditionary – Sulabh 2013-04-25 06:27:38