01.環境準備
使用的包:pymysql
pymysql下載地址:https://pypi.python.org/pypi/PyMySQL#downloads
02.pymysql安裝:
https://blog.csdn.net/u013952400/article/details/80432452
03.mysql的blob字段解釋:
轉自:https://blog.csdn.net/cbbbc/article/details/49024011
04.示例代碼:
# -*- coding=utf-8 -*-
import pymysql
import sys
#讀取圖片文件
#fp = open("test.jpg",'rb',encoding='utf-8')
fp = open("test.jpg",'rb')
img = fp.read()
fp.close()
# 創建連接
conn = pymysql.connect(host='192.168.1.128',
port=3306,
user='root',
passwd='root',
db='python_operation_01',
charset='utf8',
use_unicode=True,)
# 創建遊標
cursor = conn.cursor()
with open('test.jpg', 'rb') as fp:
data = fp.read()
cursor.execute('insert into demo_pic_repo (touxiang_data) values (%s)', [data])
# 提交,不然無法保存新建或者修改的數據
conn.commit()
# 關閉遊標
cursor.close()
# 關閉連接
conn.close()