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

在Sqlite数据库插入和读取图片数据,第1张

概述在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 18 19 20 21 22 23 24 25 26

在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
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
@H_502_63@ 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 - 1 UPDATE NulL ) ==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
@H_782_403@@H_293_404@// 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,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 );

@H_293_404@// 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
@H_293_404@// 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 ];
}
}

returndata;

}
总结

以上是内存溢出为你收集整理的在Sqlite数据库插入和读取图片数据全部内容,希望文章能够帮你解决在Sqlite数据库插入和读取图片数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1181299.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存