将图像存储到sqlite数据库中

编程入门 行业动态 更新时间:2024-10-25 06:20:41
本文介绍了将图像存储到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, modelpanyName,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,[modelpanyName 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,modelpanyName,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,[modelpanyName 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数据库中

本文发布于:2023-10-07 10:39:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   图像   sqlite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!