IOS FMDB issues with the inability to update binary data

Source: Internet
Author: User

Using Fmdb, it is convenient to implement (through database field name instead of field index) data read, INSERT, UPDATE, delete. But when I update the picture, I find that the picture cannot be displayed after the incoming binary data is updated to the database by the formatted character (@ "%@", data/nsdata/). If you use the Insert method to correctly save picture information to the database, the following method is called when the insert is processed in Fmdb

-(void) Bindobject: (ID) obj tocolumn: (int) idx instatement: (sqlite3_stmt*) pstmt {    if ((!obj) | | ((NSNull *) obj = = [NSNull null])) {        sqlite3_bind_null (pstmt, idx);    }    Fixme-someday Check the return codes on these binds.    else if ([obj Iskindofclass:[nsdata class]]) {        Sqlite3_bind_blob (pstmt, idx, [obj bytes], (int) [obj length], sqlite_s tatic);    }

This standard SQLite statement can effectively save binary information to the database

Sqlite3_bind_blob (pstmt, idx, [obj bytes], (int) [obj length], sqlite_static);

If using the UPDATE statement is not possible to invoke the above method, you can replace the OC updated SQL statement with the C language of the statement to try.

Update SQL statement for OC:

NSData *data = uiimagepngrepresentation ([UIImage imagenamed:@ "3.jpeg"]); NSString *INSERTSQL2 = [NSString stringWithFormat:                                @ "UPDATE dataTable SET '%@ ' = '%@ ' WHERE id = '%i '",                                img1, dat a,3];        BOOL res = [db EXECUTEUPDATE:INSERTSQL2];        if (res) {            NSLog (@ "Insert data successfully!) ");        }    }

Replace the updated SQL statement with C as follows:

NSData *data = uiimagepngrepresentation ([UIImage imagenamed:@ "3.jpeg"]);    nsstring* sqlstr = @ "Update new set img1 =?" WHERE id = 3 ";    BOOL res = [db executeupdate:sqlstr,data];    if (res) {        NSLog (@ "update succeeded");    }

IOS FMDB issues with the inability to update binary data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.