An error has been reported while accessing images in the mysql database using Pymysql. The code is as follows
Error message: (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\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00′ at line 1”)
I found a lot of methods on the Internet, but I couldn’t solve the problem. Later, I tried again and again and finally solved it
Note that the size of the binary BLOB in mysql is smaller than the size of the binary BLOB when designing a table, change the binary data to mediumBLOB
The code is as follows:
import pymysql
from datetime import datetime
To create the database, first connect to the mysql database
Conn = Pymysql.connect (host=’127.0.0.1′, user=’root’, Database =’ smart_apartment_DB ‘,
password=’160507pcsd’, charset=’utf8′)
Create a cursor
cursor = conn.cursor()
The # ExCute () function can execute simple SQL statements
f = open(file=’./img/imageOne.jpg’, mode=’rb’)
dataimg = f.read()
f.close()
nowtime = datetime.now().strftime(“%Y-%m-%d, %H:%M:%S”)
argdata = pymysql.Binary(dataimg)
sqlone = “insert into img_pack(imgtime,imgdata) values(‘%s’,’%s’)” % (
nowtime, argdata)
try:
cursor.execute(sqlone)
conn.commit()
except Exception as e:
conn.rollback()
Print (” Error message: “, e)
Close the cursor first
cursor.close()
Close the database connection again
conn.close()
After many attempts, the problem was found
sqlone = “insert into img_pack(imgtime,imgdata) values(‘%s’,’%s’)” % (
Remove the ‘%s’ single quotation mark from nowtime, argdata and change to
sqlone = “insert into img_pack(imgtime,imgdata) values(%s,%s)”
Cursor. Execute (SQlone) changed to CURSOR. Execute (SQlone, (nowtime, argdata))
You can store images in a binary stream like mysql
The correct code is as follows
import pymysql
from datetime import datetime
To create the database, first connect to the mysql database
Conn = Pymysql.connect (host=’127.0.0.1′, user=’root’, Database =’ smart_apartment_DB ‘,
password=’160507pcsd’, charset=’utf8′)
Create a cursor
cursor = conn.cursor()
The # ExCute () function can execute simple SQL statements
f = open(file=’./img/imageOne.jpg’, mode=’rb’)
dataimg = f.read()
f.close()
nowtime = datetime.now().strftime(“%Y-%m-%d, %H:%M:%S”)
argdata = pymysql.Binary(dataimg)
sqlone = “insert into img_pack(imgtime,imgdata) values(%s,%s)”
try:
cursor.execute(sqlone, (
nowtime, argdata)
)
conn.commit()
except Exception as e:
conn.rollback()
Print (” Error message: “, e)
Close the cursor first
cursor.close()
Close the database connection again
conn.close()
Error message: (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\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00′ at line 1”)
I found a lot of methods on the Internet, but I couldn’t solve the problem. Later, I tried again and again and finally solved it
Note that the size of the binary BLOB in mysql is smaller than the size of the binary BLOB when designing a table, change the binary data to mediumBLOB
The code is as follows:
import pymysql
from datetime import datetime
To create the database, first connect to the mysql database
Conn = Pymysql.connect (host=’127.0.0.1′, user=’root’, Database =’ smart_apartment_DB ‘,
password=’160507pcsd’, charset=’utf8′)
Create a cursor
cursor = conn.cursor()
The # ExCute () function can execute simple SQL statements
f = open(file=’./img/imageOne.jpg’, mode=’rb’)
dataimg = f.read()
f.close()
nowtime = datetime.now().strftime(“%Y-%m-%d, %H:%M:%S”)
argdata = pymysql.Binary(dataimg)
sqlone = “insert into img_pack(imgtime,imgdata) values(‘%s’,’%s’)” % (
nowtime, argdata)
try:
cursor.execute(sqlone)
conn.commit()
except Exception as e:
conn.rollback()
Print (” Error message: “, e)
Close the cursor first
cursor.close()
Close the database connection again
conn.close()
After many attempts, the problem was found
sqlone = “insert into img_pack(imgtime,imgdata) values(‘%s’,’%s’)” % (
Remove the ‘%s’ single quotation mark from nowtime, argdata and change to
sqlone = “insert into img_pack(imgtime,imgdata) values(%s,%s)”
Cursor. Execute (SQlone) changed to CURSOR. Execute (SQlone, (nowtime, argdata))
You can store images in a binary stream like mysql
The correct code is as follows
import pymysql
from datetime import datetime
To create the database, first connect to the mysql database
Conn = Pymysql.connect (host=’127.0.0.1′, user=’root’, Database =’ smart_apartment_DB ‘,
password=’160507pcsd’, charset=’utf8′)
Create a cursor
cursor = conn.cursor()
The # ExCute () function can execute simple SQL statements
f = open(file=’./img/imageOne.jpg’, mode=’rb’)
dataimg = f.read()
f.close()
nowtime = datetime.now().strftime(“%Y-%m-%d, %H:%M:%S”)
argdata = pymysql.Binary(dataimg)
sqlone = “insert into img_pack(imgtime,imgdata) values(%s,%s)”
try:
cursor.execute(sqlone, (
nowtime, argdata)
)
conn.commit()
except Exception as e:
conn.rollback()
Print (” Error message: “, e)
Close the cursor first
cursor.close()
Close the database connection again
conn.close()
Read More:
- An error keyerror255 was reported when pymysql connect to MySQL
- An error is reported when the laptop is turned on: image failed to verify with * access denied* press any key to continue
- Nuxt uses the image lazy to load the lazy component tag, and an error is reported
- When the mybatis field contains an expression, an error is reported when it is stored in the database
- An error of 500 is reported when an item is assigned to a role
- An error occurs when pymysql uses% d to pass in parameters
- When the spring MVC project is running on idea, an error is reported when the controller is a null pointer
- An error is reported when an android phone visits the app webpage: net::ERR_PROXY_CONNECTION_FAILED
- When react devtools is enabled, an error is reported for the project
- Pikachu vulnerability is installed in the shooting range, and an error is reported when connecting to the MySQL database
- An error is reported when passing data pages using Ajax: Vue is not defined
- After Vue is packaged, a blank page appears, an error is reported in the resource request, and there is no error handling scheme
- Exception record. When eclipse starts, an error is reported when workspace is opened
- An import error is reported in the python. The solution to setting. Pylintrc is invalid
- An error is reported when Clickhouse is started. The error log information is: structure needs cleaning
- [error handling] when logging into MySQL with CentOS command, an error 1045 (28000) is reported
- An error is reported when the less file is introduced into main.js
- When the springcloud obtains the cloud link database information, an error is reported: errorcode 1045, state 28000
- The imported package is gray and an error is reported
- When using freemaker template to get values, an error is reported when the attribute is empty