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