将图像存储到SQLite数据库

问题描述:

下面是我的代码存储在SQLite数据库图像。当我用它来存储它的价值,现在我试图在sqlite数据库中存储图像。我不知道我做错了什么。我已经搜查过了,我无法得到我需要的答案。任何人都可以用他的代码帮助我将图像存储到SQLite数据库

sqlite3 *database; 
    [email protected]"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: 感谢大家。我清除这个问题通过问另一个问题从这里.. store and retrieve image into sqlite database for iphone这可以帮助别人。

+0

IM有代码来读取我的appdelegate数据库,查看应用程序的同时在我的项目启动的文件.... – 2012-01-04 11:29:04

+0

请一次只问一个* *问题。 – Sathya 2012-01-04 11:33:00

+0

@Sathya OKK ......你能帮助我与我的第一question..i将编辑.. – 2012-01-04 11:34:48

我回答这个答案一个similair问题:它的更好,如果你使用CoreData代替。使用CoreDate而不是SQL可以轻松得多。 CoreData几乎是一个漂亮的包装器中的SQL数据库。

如果您使用的iOS 5,你可以图像数据库轻松添加,而不必担心他们的检查是BLOBS(二进制大对象)“允许外部存储”。

+2

我已经知道that..I必须开发应用程序的ios4..i必须使用sqlite3的..即时通讯只使用2张图片...所以我需要检查出正确的代码..不管怎么说,谢谢.. – 2012-01-04 11:57:56

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

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

哎呀我不是一个iOS开发者

+0

感谢您的答案...但它仍然不工作.. – 2012-01-04 11:38:05

+0

“不能正常工作”是一个坏评论。我们不介意读者在这里弄清楚你的屏幕上发生了什么。添加一些错误日志,至少自己做一些*基本的*调试。 @ DineshRaja.MaG – Sathya 2012-01-04 11:40:40

+0

既然你有无形的占位符,也传递参数,以便.. – nawfal 2012-01-04 11:42:48

你应该检查bind_text和bind_blob和脚步调用的返回值,如果他们无法打印错误消息。

+0

兄弟...我的代码没有执行后sqlite_prepare_v2声明..我不知道y?...任何东西即时通讯做错了? – 2012-01-04 12:04:23

+1

设置if的其他部分if(sqlite3_prepare_v2(database,...)'像'else NSLog(@“准备失败(%s)”,sqlite3_errmsg(数据库));'。这会告诉你更多的信息。 – 2012-01-04 13:32:24

你只需要添加libSqlite3.dylib到联骨架和Lilbraries并宣布数据库.h文件中可变因素

//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 *[email protected]"INSERT INTO IMAGETB(URL,image) VALUES(?,?)"; 

    if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK) 
    { 
     //NSString *url [email protected]"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg"; 
     //NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/2/2a/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG"; 
     // NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/8/84/Tibia_insulaechorab.jpg"; 


     NSString *url [email protected]"http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/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];   
} 
} 
+0

我发布我的代码。我相信它会帮助你... – 2014-09-11 10:39:52