Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中从Sqlite检索字典_Ios_Objective C_Sqlite - Fatal编程技术网

在iOS中从Sqlite检索字典

在iOS中从Sqlite检索字典,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我正在将NSMutableDictionary存储到sqlite数据库中。我成功地保存了它。我的问题是找回它。我想整体检索NSMutableDictionary并将其分配给另一个NSMutableDictionary,以便开始使用它 下面的代码允许我检索文本数据类型并将其分配给addressField,但我不知道如何从数据库检索数据类型为blob的NSMutableDictionary while (sqlite3_step(statement) == SQLITE_ROW){ NSStr

我正在将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\u column\u 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);
            }
保存后,我的数据库是这样的(我使用SQLItemManager查看数据库)。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);
    }
}

我将dict记录为它在行中的循环,这样我可以看到它,但它显示为null

sqlite3\u column\u 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];

sqlite3\u column\u 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];

谢谢你,V-Xtreme。在不再出现错误的情况下,但当我记录字典以查看字典中的内容时,它为空。你的代码中的8是什么意思?我已经编辑了我的问题。基本上,第二个参数应该是列号。试一试。当我记录字典时,仍然是空的。我将发布我的完整保存代码,保存后数据库外观的图片,以及我的检索代码。我将用此代码编辑我的原始问题。我看不出这3行提问示例代码和回答代码之间的差异。我今天面临着同样的问题,答案中也提出了同样的建议,但结果是无效的@谢谢你。在不再出现错误的情况下,但当我记录字典以查看字典中的内容时,它为空。你的代码中的8是什么意思?我已经编辑了我的问题。基本上,第二个参数应该是列号。试一试。当我记录字典时,仍然是空的。我将发布我的完整保存代码,保存后数据库外观的图片,以及我的检索代码。我将用此代码编辑我的原始问题。我看不出这3行提问示例代码和回答代码之间的差异。我今天面临着同样的问题,答案中也提出了同样的建议,但结果是无效的@V-Xtreme