2013-04-24 56 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