I am using SQLite.swift and I want to insert an UIImage in a column of type 'blob'.
My table:
"CREATE TABLE IF NOT EXISTS " + TABLE_USER +
"(" +
COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_NAME + " TEXT NOT NULL, " +
COLUMN_IMAGE + " BLOB" +
");";
After choosing an Image from gallery using UIImagePickerController, I want to save it to my "image"-column in database. I guess I have to convert the UIImage to a byte array (that's the way I did it in java in another project), but I don't know how it works in swift. That's what I tried:
// convert image
let data = image!.pngData()! as NSData
let dataBytes = data.bytes
// prepare update statement
let updateStatement = "UPDATE " + DatabaseHelper.TABLE_USER + " SET " +
DatabaseHelper.COLUMN_IMAGE + " = ? " +
" WHERE " + DatabaseHelper.COLUMN_ID + " = " + String(id)
// run statement
do {
let statement = try dbConnection.prepare(updateStatement)
try statement.run(dataBytes) // Error: Cannot convert value of type 'UnsafeRawPointer' to expected argument type 'Binding?'
} catch {
print("Error...")
}
Does anybody know how to insert the image to blob column? Thanks a lot for your help!