我正在将 NSMutableDictionary 存储到 sqlite 数据库中。我已经成功保存了。我的问题是检索它。我想检索整个 NSMutableDictionary 并将其分配给另一个 NSMutableDictionary,以便我可以开始使用它。
下面的代码允许我检索文本数据类型并将其分配给addressField,但我不知道如何从数据库中检索数据类型为blob的NSMutableDictionary。
while (sqlite3_step(statement) == SQLITE_ROW){
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
}
我不确定如何启动 NSMutableDictionary,以便我可以使用 sqlite3_column_blob 其上的功能。
这是我的保存到数据库代码。
//This is the nsdictionary that Im going to save in the database
NSMutableDictionary *thisTicket = [[NSMutableDictionary alloc]init];
[thisTicket setValue:globalData.vehicleYear forKey:@"vehicleYear"];
[thisTicket setValue:globalData.vehicleMake forKey:@"vehicleMake"];
[thisTicket setValue:globalData.vehicleModel forKey:@"vehicleModel"];
[thisTicket setValue:globalData.ticket forKey:@"ticketNumber"];
//open database and save
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_ticketDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO TICKETS (user, ticketnum, ticketinfo) VALUES (\"%@\", \"%@\", \"%@\")",
[[PFUser currentUser]objectId], globalData.ticket, thisTicket];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_ticketDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Added");
} else {
NSLog(@"Failed to add contact");
}
sqlite3_finalize(statement);
sqlite3_close(_ticketDB);
}
保存后,这是我的数据库的样子(我使用 SqliteManager 查看数据库)。 Ticketinfo 是包含我的 NSMutableDictionary 的列(在上面的代码中名为 thisTicket)。
我还没有足够的声誉来添加图片。我会赚一些然后添加它
这是我检索 NSDictionary 的代码。
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"tickets.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
}else{
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_ticketDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT ticketinfo FROM tickets WHERE user=\"%@\"",
[[PFUser currentUser] objectId]];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_ticketDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSUInteger blobLength = sqlite3_column_bytes(statement, 5);
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(statement, 5) length:blobLength];
NSDictionary *dict=[NSJSONSerialization
JSONObjectWithData: data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(@"%@",dict);
}
sqlite3_finalize(statement);
}
sqlite3_close(_ticketDB);
}
}
我 NSLogging 字典在行中循环,这样我就可以看到它,但它显示为空。
最佳答案
sqlite3_column_blob 将以 NSData 的形式为您提供 NSDictionary:
NSUInteger blobLength = sqlite3_column_bytes(statement, colNo);
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(statement, colNo) length:blobLength];
然后你必须将其转换回 NSDictionary :
NSDictionary *dictionary=[NSJSONSerialization
JSONObjectWithData: data
options:NSJSONReadingMutableLeaves
error:nil];
关于ios - 在 iOS 中从 Sqlite 检索字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24399605/