简体   繁体   中英

Retrieve dictionary from Sqlite in iOS

I am storing an NSMutableDictionary into a sqlite database. I've succeeded in storing it. My problem is retrieving it. I want to retrieve the NSMutableDictionary as a whole and assign it to another NSMutableDictionary so I can start using it.

The below code allows me to retrieve a text data type and assign it to the addressField, but I can't figure out how to retrieve an NSMutableDictionary with data type blob from the database.

while (sqlite3_step(statement) == SQLITE_ROW){
   NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)    sqlite3_column_text(statement, 0)];
}

Im not sure how to initiate the NSMutableDictionary so I can use the sqlite3_column_blob function on it.

Here is my Save to database code.

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

After I save, here is what my database look like (I use SqliteManager to view the database). Ticketinfo is the column that has my NSMutableDictionary (named thisTicket in above code).

I don't have enough reputation to add a picture yet. Ill have earn some and then add it

Here is my code to retrieve the 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);
    }
}

Im NSLogging the dict as its looping through the rows so I can see it but it displays null.

sqlite3_column_blob will gives you NSDictionary in the form of NSData:

NSUInteger blobLength = sqlite3_column_bytes(statement, colNo);
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(statement, colNo) length:blobLength];

then you have to convert it back to NSDictionary :

NSDictionary *dictionary=[NSJSONSerialization 
       JSONObjectWithData: data 
                  options:NSJSONReadingMutableLeaves 
                    error:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM