Python reports an error when storing picture binaries in mysql

with open("face.jpg","rb") as f:
    img_data=f.read()
find_binary=pymysql.Binary(img_data)
print(find_binary)
add_row="""INSERT INTO IMGS(ID,IMG,,DATAIMG) VALUES(7,"K1","NO.","%s")""" % (find_binary)
cursor.execute(add_row)

here are the reasons for the error

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00H\\x00H\\x00\\x00\\xff\\xdb\\x00C\\x00\\" at line 1")

seems to be grammatical or the output of pymysql.binary is not a binary file at all, but it is not clear what went wrong, and the DATAIMG type is mediumblob

.
May.06,2021

SQL manipulating string concatenation is not a good habit. Try parameterized query:

find_binary=pymysql.Binary(img_data)
add_row = """INSERT INTO IMGS(ID,IMG,,DATAIMG) VALUES($s, %s, %s, %s)"""
cursor.execute(add_row, (7, 'K1', 'NO.', find_binary))
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1becc87-31b2a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1becc87-31b2a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?