导入mysql.connector 从mysql.connector导入错误
def write_file(数据,文件名):
with open(filename, 'wb') as file:
file.write(data)
def readBLOB(emp_id,photo,bioData): print(“从python_employee表中读取BLOB数据”)
try:
connection = mysql.connector.connect(host='localhost',
database='image',
user='root',
password='root')
cursor = connection.cursor()
sql_fetch_blob_query = """SELECT photo from python_employee where id = %s"""
cursor.execute(sql_fetch_blob_query,(emp_id,))
record = cursor.fetchall()
for row in record:
print("Id = ", row[0],)
print("Name = ", row[1])
image = row[2]
file = row[3]
print("Storing employee image and bio-data on disk \n")
write_file(image, photo)
write_file(file, bioData)
except mysql.connector.Error as error:
print("Failed to read BLOB data from MySQL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
readBLOB(1,“ E:\ images \ krishna.jpg”, “ E:/text/eric.txt”)
readBLOB(2,“ E:\ images \ shiva.jpg”, “ E:/text/scott.txt”)