How Can I Save & Retrieve an image (bytes) to SQLite (blob) using FMDB?

后端 未结 2 889
余生分开走
余生分开走 2020-12-30 17:08

I\'m making an iOS App that need to show some images from a remote site (from an URL), and everytime the users enter to the screen that should show the image, the app get fr

相关标签:
2条回答
  • 2020-12-30 17:56

    Check out main.m in the FMDB distribution- it shows how to save and pull out a binary blob (using the safari icon as an example)".

    0 讨论(0)
  • 2020-12-30 18:04

    Methods Implementations based on fmdb.m examples

    - (NSData *)getCoverForMovie:(Movie *)movie
    {
        NSData *cover = nil;
    
        FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
    
        [db open];
        FMResultSet *results = [db executeQueryWithFormat:@"SELECT * FROM COVERS WHERE movie = %i", movie.movieID];
    
        if([results next])
        {
            cover = [results dataForColumn:@"cover"];
        }
    
        return cover;
    }
    
    
    - (BOOL)saveCover:(NSData *)cover ForMovie:(Movie *)movie
    {
        BOOL result;
    
        FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
    
        [db open];
    
        result = [db executeUpdate:@"INSERT OR REPLACE INTO COVERS (movie, cover) VALUES (?,?)", movie.movieID, cover];
    
        return result;
    }
    

    Thanks to @ccgus for his answer.

    0 讨论(0)
提交回复
热议问题