将图像存储到sqlite数据库中 [英] store images into sqlite database

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

问题描述

以下是我在sqlite数据库中存储图像的代码。当我使用它来存储值它的工作,现在我试图存储图像在sqlite数据库。我不知道我做错了什么。我已经搜索,我不能得到答案,我需要。任何人帮助我的代码。

  sqlite3 * database; 
dbName = @dataTable.sqlite;
NSArray * documentpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentdir = [documentpath objectAtIndex:0];
dbPath = [documentdir stringByAppendingPathComponent:dbName];
sqlite3_stmt * compiledStmt;
if(sqlite3_open([dbPath UTF8String],& database)== SQLITE_OK){
NSLog(@Name:%@,Company:%@,URL:%@,model.personName, model.companyName,model.imgurl);
const char * insertSQL =insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?);
if(sqlite3_prepare_v2(database,insertSQL,-1,& compiledstimt,NULL)== SQLITE_OK){
sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String], - 1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String], - 1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String], - 1,SQLITE_TRANSIENT);
NSData * imageData = UIImagePNGRepresentation(imageView.image);
sqlite3_bind_blob(compiledStmt,4,[imageData bytes],[imageData length],NULL);
NSLog(@Prepare);
sqlite3_step(compiledStmt);

} sqlite3_finalize(compiledStmt);
}

UPDATE:
* 感谢大家..我通过从这里问另一个问题清除了这个问题。将图像存储并检索到sqlite数据库for iphone

解决方案

我用这个答案回答了一个similair问题:使用CoreData。这将是更容易你使用CoreDate而不是SQL。 CoreData几乎是一个很好的包装器中的SQL数据库。



如果您使用iOS 5,您可以轻松地将图像添加到数据库,而无需担心他们是BLOBS(二进制大对象),通过选中允许外部存储。 / p>

Below is my code to store images in the sqlite database. When I used it to store values it works and now I'm trying to store images in sqlite database. I don't know what I'm doing wrong. I already searched and I can't get the answer what I need. Anyone help me with his code.

 sqlite3 *database;
    dbName=@"dataTable.sqlite";
    NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentdir=[documentpath objectAtIndex:0];
    dbPath=[documentdir stringByAppendingPathComponent:dbName];
    sqlite3_stmt *compiledStmt;
   if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
        NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl);
        const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?)";
    if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){
        sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
        sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
        sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
        NSData *imageData=UIImagePNGRepresentation(imageView.image);
        sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);
        NSLog(@"Prepare");
        sqlite3_step(compiledStmt);

    }sqlite3_finalize(compiledStmt);
}

UPDATE: *Thanks to everyone.. I cleared this issue by asked another question from here.. store and retrieve image into sqlite database for iphone This may help to others.*

解决方案

I answered a similair question with this answer: It's better if you use CoreData instead. It will be much easier for you to work with CoreDate instead of SQL. CoreData is pretty much an SQL database in a nice wrapper.

If you use iOS 5 you could easily add images to the database without having to worry about them being BLOBS (Binary Large Object) by checking "Allows External Storage".

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

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