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

后端 未结 2 910
余生分开走
余生分开走 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 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.

提交回复
热议问题