上传图片到mariadb数据库
Uploading image to mariadb database
我是 sql 的新手,所以也许这只是初学者的错误,但在尝试了我在这里和那里找到的一些解决方案之后,我仍然无法解决这个问题。我正在尝试通过 python 脚本将图像上传到 mariadb 数据库,如下所示:
import mysql.connector
path=input("File to upload: ")
with open(path,'rb') as f:
fileData=f.read()
query=f"UPDATE emp SET image={fileData} WHERE empId=1;"
db=mysql.connector.connect(...)
myCursor=db.cursor()
myCursor.execute(query)
myCursor.close()
db.close()
print(f"Uploaded {path} to the database...")
但是,当代码到达 myCursor.execute(query)
时,出现以下错误:
Traceback (most recent call last):
File "webSqlSampleClient.py", line 16, in <module>
myCursor.execute(query)
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xe1\x00'' at line 1
我的代码有什么问题?查询的其余部分似乎是正确的,因为我已经成功地将完全相同的 UPDATE
语句与其他非图像值一起使用。
预先感谢您的帮助。
-更新:
正如@tadman 所建议的,我在 sql 查询中使用 %s 作为占位符,因此新代码如下所示:
query="UPDATE emp SET image=%s WHERE empId=1"
db=mysql.connector.connect(...)
myCursor=db.cursor()
myCursor.execute(query, (fileData,))
myCursor.close()
db.close()
上面的代码不会 运行 任何异常并打印预期的输出,但是,当我在我的 SQL 客户端中执行以下 SQL 语句时:
SELECT image
FROM emp
WHERE empId = 1;
表示图像的值为NULL
。
现在怎么了? image
是一个 blob,以防不清楚。
好的,我已经开始工作了:
我用来解决第一个问题的答案是@tadman 的答案(在问题的评论中)。通过添加 db.commit()
解决了数据库未更新其值的问题(我忘记了,愚蠢的错误)。无论如何,感谢您提供的所有有用评论!
我是 sql 的新手,所以也许这只是初学者的错误,但在尝试了我在这里和那里找到的一些解决方案之后,我仍然无法解决这个问题。我正在尝试通过 python 脚本将图像上传到 mariadb 数据库,如下所示:
import mysql.connector
path=input("File to upload: ")
with open(path,'rb') as f:
fileData=f.read()
query=f"UPDATE emp SET image={fileData} WHERE empId=1;"
db=mysql.connector.connect(...)
myCursor=db.cursor()
myCursor.execute(query)
myCursor.close()
db.close()
print(f"Uploaded {path} to the database...")
但是,当代码到达 myCursor.execute(query)
时,出现以下错误:
Traceback (most recent call last):
File "webSqlSampleClient.py", line 16, in <module>
myCursor.execute(query)
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/asriel/.local/lib/python3.8/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xe1\x00'' at line 1
我的代码有什么问题?查询的其余部分似乎是正确的,因为我已经成功地将完全相同的 UPDATE
语句与其他非图像值一起使用。
预先感谢您的帮助。
-更新:
正如@tadman 所建议的,我在 sql 查询中使用 %s 作为占位符,因此新代码如下所示:
query="UPDATE emp SET image=%s WHERE empId=1"
db=mysql.connector.connect(...)
myCursor=db.cursor()
myCursor.execute(query, (fileData,))
myCursor.close()
db.close()
上面的代码不会 运行 任何异常并打印预期的输出,但是,当我在我的 SQL 客户端中执行以下 SQL 语句时:
SELECT image
FROM emp
WHERE empId = 1;
表示图像的值为NULL
。
现在怎么了? image
是一个 blob,以防不清楚。
好的,我已经开始工作了:
我用来解决第一个问题的答案是@tadman 的答案(在问题的评论中)。通过添加 db.commit()
解决了数据库未更新其值的问题(我忘记了,愚蠢的错误)。无论如何,感谢您提供的所有有用评论!