Xcode 无法将映像添加到sqlite数据库?

Xcode 无法将映像添加到sqlite数据库?,xcode,image,sqlite,add,Xcode,Image,Sqlite,Add,有代码可用于将图像插入其他字段: UIImage *image1 = [UIImage imageNamed:@"recette.jpg"]; NSData *image2 = UIImagePNGRepresentation(image1); if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { NSString *insertSQL = [NSString stringWit

有代码可用于将图像插入其他字段:

UIImage *image1 = [UIImage imageNamed:@"recette.jpg"];
            NSData *image2 = UIImagePNGRepresentation(image1);

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (name, address, phone,image) VALUES (\"%@\", \"%@\", \"%@\", \"%@\" )", name.text, address.text, phone.text,image2];

认为需要将图像添加为blob对象

UIImage *image1 = [UIImage imageNamed:@"recette.jpg"];
NSData *imgData = UIImagePNGRepresentation(image1);
sqlite3_stmt *stmt;

char *update = "INSERT INTO CONTACTS (name, address, phone,image) VALUES (?, ?, ?, ?);";

if (sqlite3_prepare_v2(dbpath, update, -1, &stmt, nil) == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, name.text, -1, NULL);
        sqlite3_bind_text(stmt, 2, address.text, -1, NULL);
        sqlite3_bind_text(stmt, 3, phone.text, -1, NULL);
        if(imgData != nil)
            sqlite3_bind_blob(stmt, 4, [imgData bytes], [imgData length], NULL);
        else
            sqlite3_bind_blob(stmt, 4, nil, -1, NULL);

 }
 else if (sqlite3_step(stmt) != SQLITE_DONE){
     char *errorMsg;
     NSAssert1(0, @"Error updating table: %s", errorMsg);
}

    sqlite3_finalize(stmt);

我没有遗漏任何东西,因为它在没有图像的情况下工作,我只是放了一些代码来解释,这不是整个代码+1,但有时只存储图像的位置可能更好。(这样,图像就不必发送太多)它工作得很好很抱歉我更改了bd的名称,它工作得很好:)