想要在Sqlite中插入完整的JSON响应数据(字典)并使用字典响应进行检索

时间:2016-11-09 18:05:39

标签: ios json sqlite

我尝试过多种解决方案 1)另存为NSData 2)另存为NSString 3)尝试使用NSKeyedUnarchiver

在我的案例中没有任何作用。检查附加的JSON数据

从sqlite保存和检索数据的代码

- (BOOL)saveBookData:(NSString *)bookID bookName:(NSString*)bookNames bookTitle:(NSString*)bookTitle valueArr:(NSDictionary *)valueArr
  {
   const char *dbpath = [databasePath UTF8String];
   if (sqlite3_open(dbpath, &database) == SQLITE_OK)
   {
    NSData *theDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:valueArr];
    NSString *insertSQL = [NSString stringWithFormat:@"insert into bookDetails(bookID, bookName, bookTitle, valueArr) values (\"%@\",\"%@\", \"%@\", \"%@\")",bookID,bookNames,bookTitle, theDictionaryData];
    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);

   // sqlite3_bind_text(statement, 1, [bookID UTF8String], -1,  SQLITE_TRANSIENT);
    sqlite3_bind_text(statement, 2, [bookNames UTF8String], -1,  SQLITE_TRANSIENT);
    sqlite3_bind_text(statement, 3, [bookTitle UTF8String], -1,  SQLITE_TRANSIENT);
    //sqlite3_bind_text(statement, 4, [valueArr UTF8String], -1,  SQLITE_TRANSIENT);
    sqlite3_bind_blob(statement, 4, (__bridge const void *)(theDictionaryData) , -1, SQLITE_TRANSIENT);



    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        return YES;
    }
    else {
        return NO;
    }
    //sqlite3_reset(statement);
}
return NO;
}

retrive -

-(NSMutableArray*)findAllBook
{
resultArray = [[NSMutableArray alloc] init];

sqlite3 *database2;

if(sqlite3_open([databasePath UTF8String], &database2) == SQLITE_OK)
{

    NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select bookID, bookName, bookTitle, valueArr from bookDetails"];
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
    {
        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {
            NSMutableDictionary *_dataDictionary2=[[NSMutableDictionary alloc] init];

            NSString *_savePdfID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
            NSString *_savePdfName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *_savePdfText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            // NSString *_savePdfDetails = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
            NSData *retrieveData = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 4) length:sqlite3_column_bytes(compiledStatement, 4)];

            //NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:retrieveData];
            //NSLog(@"dict data is %@",dictionary);

            [_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_savePdfID] forKey:@"savedBOOKID"];
            [_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_savePdfName] forKey:@"savedBOOKName"];
            [_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_savePdfText] forKey:@"savedBOOKText"];
            [_dataDictionary2 setObject:retrieveData forKey:@"savedBOOKDetail"];

            [resultArray addObject:_dataDictionary2];
        }
    }
    else
    {

    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database2);

return resultArray;
 }

我在控制器中使用的代码

    NSLog(@"%@",[[PDFManager getSharedInstance]findAllBook]);
NSMutableArray *arr = [[[PDFManager getSharedInstance]findAllBook]valueForKey:@"savedBOOKDetail"];
NSLog(@"%@",arr);
NSMutableDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:[arr objectAtIndex:0]];
NSLog(@"%@",dictionary);

盖伊我已多次尝试过,但仍然遇到问题,在回复时总是得到空值,请你提供一些解决方案来解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的代码存在许多问题。但问题的主要原因是valueArr列的列索引号。当您尝试读取数据时,使用错误的索引。

您的查询是:

  

从bookDetails

中选择bookID,bookName,bookTitle,valueArr

bookdID是索引0而valueArr是索引3.所有sqlite3_column_xxx函数都使用基于零的索引,即使所有sqlite3_bind_xxx函数都使用基于零的索引索引。

这一行:

NSData *retrieveData = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 4) length:sqlite3_column_bytes(compiledStatement, 4)];

需要:

NSData *retrieveData = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)];

除此之外,您还需要解决saveBookData方法中的所有问题。如果数据库被打开,您就不会关闭数据库。在准备语句时,您不会进行任何错误检查。如果准备好了,你就不会敲定声明。

您的findAllBook更好,但仍有问题。即使数据库没有打开,也会关闭它。即使它没有准备好,你也要完成声明。当任何错误检查失败时,您应该向sqlite3_errmsg添加调用,以便了解它失败的原因。