如何在sqlite中存储图像 [英] how to store image in 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屋!