iOS sqlite数据库插入和读取图片数据

来源:互联网 发布:网页html编辑器源码 编辑:程序博客网 时间:2024/04/28 08:11

在iOS下用sqlite数据库存储图片,先把你的图片转换成 NSData 形式,然后在数据库添加一行 blob 数据

假定数据库中存在表 test_table(name,image), 下面代码将图片文件test.png的二进制数据写到sqlite数据库:
01    char *name = “test”;
02    NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
03    NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString ofType:@”png”];
04    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
05    {


06        NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);
07        const  char * sequel = “insert into test_table(name,image) values(?,?)”;
08
09        sqlite3_stmt * update;
10        if (sqlite3_prepare_v2(database, sequel, -1, &update, NULL) == SQLITE_OK)
11        {
12            sqlite3_bind_text(update, 1, name, -1, NULL);
13            sqlite3_bind_blob(update, 2, [imgData bytes], [imgData length], NULL);
14            if( sqlite3_step(update) == SQLITE_DONE)
15            {
16               NSLog(@”已经写入数据”);
17            }
18            sqlite3_finalize(update);
19        }
20    }
21    else
22    {
23        NSLog(@”文件不存在”);
24    }
25
26    下面代码从数据库中读取图片二进制数据,然后显示图片:
27    const char * sequel = “select image from test_table where name=?”;
28    sqlite3_stmt * getimg;
29    if (sqlite3_prepare_v2(database, sequel, -1, &getimg, NULL) == SQLITE_OK)
30    {
31        char *name = “test”;
32        sqlite3_bind_text(update, 1, name, -1, NULL);
33        if(sqlite3_step(getimg)  == SQLITE_ROW)
34        {
35            int bytes = sqlite3_column_bytes(getimg, 0);
36            Byte * value = (Byte*)sqlite3_column_blob(getimg, 1);
37            if (bytes !=0 && value != NULL)
38            {
39                NSData * data = [NSData dataWithBytes:value length:bytes];
40                UIImage * img = [UIImage imageWithData:data];
41                UIImageView * aview =[[UIImageView alloc] initWithFrame:
42                                        CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
43                aview.image = img;
44                [self.view addSubview:aview];
45                [aview release];
46             }
47         }
48         sqlite3_finalize(getimg);
49    }

例2:

存储图片:

01    // Save Small Image Data by given main url
02    - (void) SaveImagesToSql: (NSData*) imgData :( NSString*) mainUrl
03    {
04        NSLog( @”\n*****Save image to SQLite*****\n” );
05
06        const char* sqliteQuery = “INSERT INTO IMAGES (URL, IMAGE) VALUES (?, ?)”;
07        sqlite3_stmt* statement;
08
09        if( sqlite3_prepare_v2(articlesDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
10        {
11            sqlite3_bind_text(statement, 1, [mainUrl UTF8String], -1, SQLITE_TRANSIENT);
12            sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
13            sqlite3_step(statement);
14        }
15        else NSLog( @”SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s”, sqlite3_errmsg(articlesDB) );
16
17        // Finalize and close database.
18        sqlite3_finalize(statement);
19    }
读取图片:

01    // Load images from data base with given image url
02    - (NSData*) LoadImagesFromSql: (NSString*) imageLink
03    {
04        NSData* data = nil;
05        NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT IMAGE FROM IMAGES WHERE URL = '%@'", imageLink];
06        sqlite3_stmt* statement;
07
08        if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )
09        {
10            if( sqlite3_step(statement) == SQLITE_ROW )
11            {
12                int length = sqlite3_column_bytes(statement, 0);
13                data       = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
14            }
15        }
16
17        // Finalize and close database.
18        sqlite3_finalize(statement);
19
20        return data;
21
22    }