pymysql更新图片出现错误

obj = open(r'captcha.jpg', 'rb').read()
sql = f"update personal_picture set picture='{obj}' where pp_name='{c}';"
cursor = ali_per_conn.cursor()
cursor.execute(sql)

代码如上,想把一个图片更新到数据库,总是出现如下错误

    err.raise_mysql_exception(self._data)
  File "C:\Users\25808\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
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\\x02\\x00\\x00\\x01\\x00\\x01\\x00\\x00\\xff\\xdb\\x00' at line 1")

 

请大神指点一下

err.raise_mysql_exception 两个用法示例,供参考:

示例1: test_raise_mysql_exception

 

# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import raise_mysql_exception [as 别名]
def test_raise_mysql_exception(self):
        data = b"\xff\x15\x04Access denied"
        with self.assertRaises(err.OperationalError) as cm:
            err.raise_mysql_exception(data)
        self.assertEqual(cm.exception.args, (1045, 'Access denied')) 

示例2: test_raise_mysql_exception_client_protocol_41

# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import raise_mysql_exception [as 别名]
def test_raise_mysql_exception_client_protocol_41(self):
        data = b"\xff\x15\x04#28000Access denied"
        with self.assertRaises(err.OperationalError) as cm:
            err.raise_mysql_exception(data)
        self.assertEqual(cm.exception.args, (1045, 'Access denied')) 

 

解决方式如下:
1.首先把数据库字符集设置utf8mb4
在这里插入图片描述
2.连接数据库时charset="utf8mb4"
在这里插入图片描述