我正在创建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);
}
但我得到了严重的额外异常。请帮助我检索数据