如何在sqlite中存储图像 [英] how to store image in sqlite

查看:111
本文介绍了如何在sqlite中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite3 *数据库;

sqlite3_stmt * compiledStmt;

//初始化动物数组

NSString * docsDir;

NSArray * dirPaths;

//获取文档目录

dirPaths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory,NSUserDomainMask,YES);

docsDir = dirPaths [0];

//构建数据库文件的路径

databasePath = [[NSString alloc] initWithString:

[docsDir stringByAppendingPathComponent:student.db]];



//从用户filessytem打开数据库

if( sqlite3_open([databasePath UTF8String],& database)== SQLITE_OK)

{



NSString * insertSQL = insert into studentsDetail(image)值(?);

if(sqlite3_prepare_v2(数据库,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding],-1,& compiledStmt,NULL)== SQLITE_OK)

{

NSLog(@" before");

UIImage * image = [UIImage imageNamed:@" Cameroon.png"];

NSData * imageData = UIImagePNGRepresentation(image);



sqlite3_bind_blob(compiledStmt,1,[imageData bytes],[imageData length],SQLITE_TRANSIENT);

if(sqlite3_step(compiledStmt)!= SQLITE_DONE){

NSLog(@错误:%s",sqlite3_errmsg(数据库));

} else {

NSLog(@" Insert into row id =%lld",(sqlite3_last_insert_rowid(database)));



}

sqlite3_finalize(compiledStmt);

}

}

sqlite3_close(数据库);



代码中有什么问题...

解决方案

调试代码并找出究竟是什么问题?



在leas得到错误,所以有些可以帮助你继续前进。



同时检查这篇CP文章:

C#& SQLite - 存储图像 [ ^ ]



..和类似答案 here [ ^

sqlite3 *database;
sqlite3_stmt *compiledStmt;
// Init the animals Array
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent:student.db]];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{

NSString *insertSQL=insert into studentsDetail(image) values(?);
if (sqlite3_prepare_v2(database, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStmt, NULL)== SQLITE_OK)
{
NSLog(@"before");
UIImage *image = [UIImage imageNamed:@"Cameroon.png"];
NSData *imageData=UIImagePNGRepresentation(image);

sqlite3_bind_blob(compiledStmt, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
if(sqlite3_step(compiledStmt) != SQLITE_DONE ) {
NSLog( @"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( @"Insert into row id = %lld", (sqlite3_last_insert_rowid(database)));

}
sqlite3_finalize(compiledStmt);
}
}
sqlite3_close(database);

what is wrong in the code...

解决方案

Debug your code and find what exactly the problem is?

At least get the error so some can help you move on.

Also check this CP article on same:
C# & SQLite - Storing Images[^]

..and similar answer here[^]


这篇关于如何在sqlite中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆