objective-c – 对nscmage的blob和blob的NSImage(SQLite,NSData)

前端之家收集整理的这篇文章主要介绍了objective-c – 对nscmage的blob和blob的NSImage(SQLite,NSData)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我知道我不应该使用sqlite数据库来存储图像,但我只存储非常小的网站图标.

我的问题是我尝试将这些favicon插入数据库(似乎工作),我使用NSimage的-tiffrepresentation方法将favicon转换为NSData,然后将其插入我的数据库中的blob列:

NSImage *favico = [webview mainFrameIcon];
[appDelegate insertBookmark:[titleField stringValue] url:[urlfield stringValue] data:[favico TIFFRepresentation]]

sqlite方法如下所示:

-(void)insertBookmark:(NSString *)title url:(NSString *)url data:(NSData *)data
{
    NSData *imagedata = [[NSData alloc]initWithData:data]; 
    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String],&database) == sqlITE_OK) {
        NSString *query = [NSString stringWithFormat:@"INSERT INTO Bookmarks (title,url,image) VALUES ('%@','%@','%@')",title,data];
        const char *querychar = [query UTF8String]; 
        sqlite3_stmt *statement; 
        if (sqlite3_prepare_v2(database,querychar,-1,&statement,NULL) == sqlITE_OK)
        {
            int row =  3;
            sqlite3_bind_blob(statement,row,[imagedata bytes],[imagedata length],NULL);
            sqlite3_step(statement);
            sqlite3_finalize(statement);

        }
        else
        {
            NSLog(@"Error"); 
        }

        [databasePath retain]; 
        [databaseName retain]; 
    }
    sqlite3_close(database);
    [imagedata release]; 

}

当我查看数据库时,我在< data>之间的image列中有值. (正常吗?)

现在,当我从数据库提取blob并尝试将其放入我的对象时,我在NSimage中得到了null:

-(void) readDatabase {
// Setup the database object
sqlite3 *database;

bookmarks = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String],&database) == sqlITE_OK) {
    // Setup the sql Statement and compile it for faster access
    const char *sqlStatement = "SELECT * FROM Bookmarks ORDER BY title ASC";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database,sqlStatement,&compiledStatement,NULL) == sqlITE_OK) {
        // Loop through the results and add them to the Feeds array
        while(sqlite3_step(compiledStatement) == sqlITE_ROW) {
            // Read the data from the result row
            NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,1)];
            NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)];
            NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement,3) length:sqlite3_column_bytes(compiledStatement,3)];
            NSImage *image = [[NSImage alloc]initWithData:data];
            bookmarkObject *bookmark = [[bookmarkObject alloc] initWithName:aTitle url:aUrl favico:image]; 

            [bookmarks addObject:bookmark];
            [bookmark release];
            [data release];
            [image release]; 



        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
    sqlite3_close(database); 
    [databasePath retain]; 
    [databaseName retain]; 


}

}

提前致谢

解决方法

<和>您的实际数据来自NSData.通过在格式字符串中使用%@并提供数据,NSString将描述发送到数据. [NSData description]将内容包装在<和>.

您的代码的另一个问题是,您似乎混合了无参数和&参数预处理语句:

>使用stringWithFormat:创建完整的查询语句
>或使用类似INSERT INTO书签(标题…)的查询VALUES(?…)与sqlite3_bind_blob结合使用

sqlite支持的参数语法可以在这里找到:
http://www.sqlite.org/c3ref/bind_blob.html

原文链接:https://www.f2er.com/c/119099.html

猜你在找的C&C++相关文章