pymysql.err.programmingError: (1064)



我坚持将blob存储到xampp服务器中。这是我得到的错误。

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '5\xd2\xe0\xe5yllN\xbc\xa7!\x11!\x16\xcftaJ\xd1\x863\xbf\x9a\x9aF\x83\xe8\xc9\...' at line 2")

这个函数应该将blob保存到xampp中。xampp中的数据类型设置为mediumblob。我正在使用tkinter,这个函数被绑定到一个按钮。

def save_to_db():
get_id_no = id_no_var.get()
get_first_name = first_name_var.get()
get_middle_name = middle_name_var.get()
get_last_name = last_name_var.get()
get_course = course_var.get()
raw_qr_code_id = str(get_id_no + get_first_name + get_middle_name + get_last_name + get_course)
final_qr_code_id = str(raw_qr_code_id.replace(" ", ""))
filename = (final_qr_code_id + ".png")
raw_image = (filename)
image = open(raw_image, 'rb')
image_binary = image.read()
cursor.execute("""INSERT INTO `student_information` (`id_no`, `first_name`, `middle_name`, `last_name`, `course`, `qr_code_id`, `qr_code_blob`)
VALUES  ('%s', '%s', '%s', '%s', '%s', '%s', '%s')""" % (get_id_no, get_first_name, get_middle_name, get_last_name, get_course, final_qr_code_id, image_binary))
connect_db.commit()

我是如何设法插入数据的:我首先创建了一个表(使用LONGBLOB类型,因为我的图片的大小):

MariaDB [DB]> create table TEST ( id int, file_file LONGBLOB);

然后对图片进行二值化处理

image = open('/home/med/Pictures/Screenshot from 2019-12-19 12-48-53.png', 'rb')
image_binary = image.read()

之后,我使用了以下查询(注意查询周围的单引号和%s周围的双引号)

connection.execute('''INSERT INTO TEST (`id`, `file_file`) VALUES ("%s", "%s")''', (1, image_binary))
# <sqlalchemy.engine.cursor.LegacyCursorResult at 0x7f2b24165580>

检查注射是否成功

MariaDB [DB]> select count(*) from TEST;                      
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.001 sec)
MariaDB [DB]> 

最新更新