无法从ios应用程序中的sqlite数据库中检索图像

时间:2014-01-23 09:28:34

标签: ios sqlite

我试图将图像(BLOB数据类型)插入到sqlite数据库中。

当我试图检索时,图像视图中没有显示任何内容。我将插入并正确显示插入数据库的所有其他详细信息,如firstnane,lastname,电话号码。

* 我的插入代码。 *     sqlite3_stmt *语句;     const char * dbpath = [databasePath UTF8String];     NSData * imageData = UIImagePNGRepresentation(addPhoto.image);

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS(PHONE,FNAME,LNAME,EMAIL,IMAGE) VALUES (\"%@\", \"%@\", \"%@\",\"%@\",\"%@\")",
                           phoneText.text,fNameText.text, lNameText.text,emailText.text,imageData];
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(contactDB, insert_stmt,-1, &statement, NULL);
    sqlite3_bind_text(statement, 1, [phoneText.text UTF8String], -1, NULL);
    sqlite3_bind_text(statement, 2, [fNameText.text UTF8String], -1, NULL);
    sqlite3_bind_text(statement, 3, [lNameText.text UTF8String], -1, NULL);
    sqlite3_bind_text(statement, 4, [emailText.text UTF8String], -1, NULL);
    sqlite3_bind_blob(statement, 5, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

    if (sqlite3_step(statement) == SQLITE_DONE)
    {
      // Some message
    } else {
        //Some message  
           }
   sqlite3_finalize(statement);
   sqlite3_close(contactDB);
}

从数据库中检索的代码

             NSString *select_sql= [NSString stringWithFormat:@"Select FNAME,LNAME,EMAIL,PHONE,IMAGE from CONTACTS where FNAME='%@'",nam1];
                const char *select_stmt =[select_sql UTF8String];
                sqlite3_stmt *compiledStatement;
                int res = SQLITE_ERROR;//
                int len = 0;//
                if (sqlite3_prepare_v2(contactDB,select_stmt, -1, &compiledStatement, NULL) == SQLITE_OK)
                {
                        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                        NSString *selectedFirstName=[NSString stringWithFormat: @"%s",(const char *)sqlite3_column_text(compiledStatement,0)];
                        NSString *selectedLastName=[NSString stringWithFormat: @"%s",(const char *)sqlite3_column_text(compiledStatement,1)];
                        NSString *selectedEmail=[NSString stringWithFormat: @"%s",(const char *)sqlite3_column_text(compiledStatement,2)];
                        NSString *selectedPhone=[NSString stringWithFormat: @"%s",(const char *)sqlite3_column_text(compiledStatement,3)];

 len = sqlite3_column_bytes(compiledStatement, 4);
 NSData *imgData = [[NSData alloc] initWithBytes: 

sqlite3_column_blob(compiledStatement, 4) length: len];

                     UIImage *img = [[UIImage alloc] initWithData:imgData];  

                        imageLabel.image = img;
                        [self.view addSubview:imageLabel];
                        fNameLabel.text=selectedFirstName;
                    }
                }
                else
                {

                }


            }

0 个答案:

没有答案