2013-04-29 49 views
-1

我正在創建使用SQLite數據庫的iOS應用程序。 我已經創建表:NSCocoaErrorDomain代碼= 3840問題,同時檢索NSDATA到NSDictionary

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

而且我得到錯誤的

錯誤域= NSCocoaErrorDomain代碼= 3840 「操作不可能是 已完成(可可錯誤3840.)」(JSON文本未以數組或 對象和選項允許片段未設置。)

+0

和我插入的NSDictionary爲 示例: po self.selectedItemsDictnory $ 1 = 0x07184170 { Light = 50; 麪食= 220; } 我試圖搜索這個錯誤。我發現我正在檢索的JSON實際上不是JSON格式。 請提供您的輸入謝謝 – Sulabh 2013-04-29 13:24:45

回答

1

您未格式化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]; 
相關問題