将图像数据保存到iphone中的sqlite数据库

时间:2011-02-18 09:01:00

标签: ios image sqlite save

我想将图像数据保存到我的sqlite数据库,但我不能这样做......

- (void) SaveData: (Article*) article :(NSMutableArray*) rssList
{
    sqlite3_stmt *statement;

    if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK)
    {
        NSString* sqlFormat = @"INSERT INTO ARTICLES (guid, title, summary, mainLink, pubDate, author, imageLink, body, favorites, smallImage) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', ?)";
        // Correct summary.
        NSString* summary = [NSString stringWithString:article.summary];
        summary           = [summary stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
        // Correct title
        NSString* title   = [NSString stringWithString:article.title];
        title             = [title stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
        // Correct Body
        NSString* body    = [NSString stringWithString:article.body];
        //body              = [body stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
        // Create Insert SQL query.
        NSString* insertSQL = [NSString stringWithFormat:sqlFormat, article.guid, title, summary, article.mainLink, article.pubDate, article.author, article.imageLink, body, article.favorite ? @"YES" : @"NO"];
        // Add to database.
        sqlite3_prepare_v2(articlesDB, [insertSQL UTF8String], -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            // Add to list.
            [rssList addObject:article];
            // Write Log.
            NSLog( @"Article added ... " );
        } 
        else
        {
            NSLog( @"Failed to add article. Error is:  %s", sqlite3_errmsg(articlesDB) );
        }

        sqlite3_finalize(statement);
        sqlite3_close(articlesDB);
    }
}

你可以看到我有查询,最后我有小图像和值(?)

@"INSERT INTO ARTICLES (guid, title, summary, mainLink, pubDate, author, imageLink, body, favorites, smallImage) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', ?)";

我有一个必须在程序中某处更新图像值的函数:在这个函数中,当我尝试将数据写入最后一列“SMALLIMAGE”但没有结果时,我选择带有图像链接url的行...当我用编辑器打开我的数据库文件我在最后一行只看到“NULL”,就像我的数据没有保存...

- (void) SaveSmallImage: (NSData*) imgData :(NSString*) mainUrl
{
    if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK) 
    {
        NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT * FROM ARTICLES WHERE IMAGELINK =  '%@'", mainUrl]; 
        sqlite3_stmt* statement;

        if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK ) 
        {
            sqlite3_bind_blob(statement, 9, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
            sqlite3_step(statement);
        }
        else
        {
            NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(articlesDB) );
        }

        // Finalize and close database.
        sqlite3_finalize(statement);
        sqlite3_close(articlesDB);
    }
    else 
    {
        NSLog(@"SaveSmallImage: Failed from sqlite3_open. Error is: %s", sqlite3_errmsg(articlesDB) );
    }
}

谁能告诉我我做错了什么......

3 个答案:

答案 0 :(得分:16)

Here is how you can Save image to SQLITE

// Create Images Table.
sql_stmp = "CREATE TABLE IF NOT EXISTS IMAGES (URL TEXT UNIQUE, IMAGE BLOB)";
if( sqlite3_exec(articlesDB, sql_stmp, NULL, NULL, &errMsg) != SQLITE_OK )
    NSLog( @"Fail to create \"IMAGES\" table. Error is: %@", errMsg );

这是保存功能:

// Save Small Image Data by given main url 
- (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl
{
    NSLog( @"\n*****Save image to SQLite*****\n" );

    const char* sqliteQuery = "INSERT INTO IMAGES (URL, IMAGE) VALUES (?, ?)";
    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(articlesDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
    {
        sqlite3_bind_text(statement, 1, [mainUrl UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
        sqlite3_step(statement);
    }
    else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(articlesDB) );

    // Finalize and close database.
    sqlite3_finalize(statement);
}

这是加载图片的功能:

// Load images from data base with given image url 
- (NSData*) LoadImagesFromSql: (NSString*) imageLink
{
    NSData* data = nil;
    NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT IMAGE FROM IMAGES WHERE URL = '%@'", imageLink];    
    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
    {
        if( sqlite3_step(statement) == SQLITE_ROW )
        {
            int length = sqlite3_column_bytes(statement, 0);
            data       = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
        }
    }

    // Finalize and close database.
    sqlite3_finalize(statement);

    return data;

}           

答案 1 :(得分:3)

或者我如何更新blob数据?

NSString* sqliteQuery = [NSString stringWithFormat:@"UPDATE ARTICLES SET SMALLIMAGE = '?' WHERE IMAGELINK = '@%'", mainUrl];

功能是:

if (sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK) 
        {
            NSString* sqliteQuery = [NSString stringWithFormat:@"UPDATE ARTICLES SET SMALLIMAGE = '?' WHERE IMAGELINK = '@%'", mainUrl];    
            sqlite3_stmt* statement;

            if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK ) 
            {
                int result = sqlite3_bind_blob(statement, 9, [imgData bytes], [imgData length], NULL);
                sqlite3_step(statement);
            }
            else
            {
                NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(articlesDB) );
            }

            // Finalize and close database.
            sqlite3_finalize(statement);
            sqlite3_close(articlesDB);
        }
        else 
        {
            NSLog(@"SaveSmallImage: Failed from sqlite3_open. Error is: %s", sqlite3_errmsg(articlesDB) );
        }

这部分代码返回错误25

int result = sqlite3_bind_blob(statement, 9, [imgData bytes], [imgData length], NULL);

答案 2 :(得分:0)

更新BLOB图像:(编辑从下面的更新答案中纠正一些拼写错误)

-(void) UpdateImagesToSql: (NSData *)imgData forUrl: (NSString *) mainUrl {

    NSLog( @"\n*****Updating image to SQLite*****\n",);

    const char* sqliteQuery =  NSString* sqliteQuery = [NSString stringWithFormat:@"UPDATE ARTICLES SET SMALLIMAGE = ? WHERE IMAGELINK = '@%'", mainUrl]; 

    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(articlesDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
    {
        sqlite3_bind_blob(statement, 1, [imgData bytes], (int)[imgData length], SQLITE_TRANSIENT);
        sqlite3_step(statement);
    }
    else
    {
        NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(articlesDB) );
    }

    // Finalize and close database.

    sqlite3_finalize(statement);
}