加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Sqlite数据库插入和读取图片数据

发布时间:2020-12-12 20:14:50 所属栏目:百科 来源:网络整理
导读:在iOS下用sqlite数据库存储图片,先把你的图片转换成 NSData 形式,然后在数据库添加一行 blob 数据 假定数据库中存在表 test_table(name,image),下面代码将图片文件test.png的二进制数据写到sqlite数据库: 例1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 CHAR*name ="test";
NSString *nameString =[NSString stringWithCString:name encoding:NSUTF8StringEncoding ];
NSString *filePath [[NSBundle mainBundle ]pathForResource:nameString ofType:@ "png"];
IF([NSFileManager defaultManager ]fileExistsAtPath:filePath ])
{
NSData *imgData =UIImagePNGRepresentation [UIImage imageWithContentsOfFile:filePath );
const *sequel "insert into test_table(name,image) values(?,?)";

sqlite3_stmt *UPDATE;
(sqlite3_prepare_v2 (DATABASE,sequel -1UPDATENULL)==SQLITE_OK )
{
sqlite3_bind_text name );
sqlite3_bind_blob 2 [imgData bytes [imgData LENGTH );
(sqlite3_step ==SQLITE_DONE )
{
NSLog (@ "已经写入数据" );
}
sqlite3_finalize );
}
}
ELSE
{
NSLog "文件不存在" );
}

下面代码从数据库中读取图片二进制数据,然后显示图片:
const "select image from test_table where name=?";
sqlite3_stmt *getimg;
{
"test";
sqlite3_bind_text );
(getimg ==SQLITE_ROW {
INTbytes =sqlite3_column_bytes 0 );
Byte VALUE (Byte * )sqlite3_column_blob (bytes ! = 0&& VALUE! = {
NSData DATA [NSData dataWithBytes: VALUE LENGTH:bytes ];
UIImage *img [UIImage imageWithData: ];
UIImageView *aview [UIImageView alloc ]initWithFrame:
                                    CGRectMake ( 0.0 IMAGE_HEIGHT ];
aview .image =img;
[ SELF . VIEWaddSubview:aview ];
[aview release ];
}
}
sqlite3_finalize }

例2:存储图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 // Save Small Image Data by given main url
-(void)SaveImagesToSql :(NSData*)imgData NSString)mainUrl
{
NSLog (@"n*****Save image to SQLite*****n");

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

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

// Finalize and close database.
sqlite3_finalize );
}

读取图片:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 // Load images from data base with given image url
)LoadImagesFromSql )imageLink
{
*data =nil;
=[NSStringstringWithFormat :"SELECT IMAGE FROM IMAGES WHERE URL = '%@'",imageLink ];
sqlite3_stmt [sqliteQuery UTF8String {
(sqlite3_step ==SQLITE_ROW )
{
intlength =sqlite3_column_bytes 0);
data NSDatadataWithBytes :sqlite3_column_blob )length :length ];
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

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

假定数据库中存在表 test_table(name,image),下面代码将图片文件test.png的二进制数据写到sqlite数据库:

例1

    推荐文章
      热点阅读