欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python中怎么向mysql中存儲圖片-創(chuàng)新互聯(lián)

這篇文章給大家介紹python中怎么向mysql中存儲圖片,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)主營泗縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā)公司,泗縣h5重慶小程序開發(fā)公司搭建,泗縣網(wǎng)站營銷推廣歡迎泗縣等地區(qū)企業(yè)咨詢

示例代碼如下:


#!/usr/bin/python

# -*- coding: utf-8 -*-
import MySQLdb as mdb
import systry:
    fin = open("chrome.png")
    img = fin.read()
    fin.close()

except IOError, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)try:
    conn = mdb.connect(host='localhost',user='testuser',
       passwd='test623', db='testdb')
    cursor = conn.cursor()
    cursor.execute("INSERT INTO Images SET Data='%s'" % \
        mdb.escape_string(img))
    conn.commit()
    cursor.close()
    conn.close()

except mdb.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

python中怎么向mysql中存儲圖片

首先,我們打開一個圖片,并讀取圖片數(shù)據(jù),代碼如下:

fin = open("chrome.png")

img = fin.read()

接著,我們把圖片數(shù)據(jù)插入到數(shù)據(jù)庫中,并使用escape_string進(jìn)行特殊字符串轉(zhuǎn)義。代碼如下:

cursor.execute("INSERT INTO Images SET Data='%s'" % \

    mdb.escape_string(img))

(十三)讀取圖片

上一節(jié)中,我們把圖片存儲到數(shù)據(jù)庫中了,在本節(jié),我們將取回并保存為圖片文件。本節(jié)示例如下:

python中怎么向mysql中存儲圖片

#!/usr/bin/python

# -*- coding: utf-8 -*-
import MySQLdb as mdb 
import systry:
    conn = mdb.connect(host='localhost',user='testuser', 
        passwd='test623', db='testdb')
    cursor = conn.cursor()
    cursor.execute("SELECT Data FROM Images LIMIT 1")
    fout = open('image.png','wb')
    fout.write(cursor.fetchone()[0])
    fout.close()
    cursor.close()
    conn.close()

except IOError, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

python中怎么向mysql中存儲圖片

首先,我們從數(shù)據(jù)庫中讀取一張圖片數(shù)據(jù):

cursor.execute("SELECT Data FROM Images LIMIT 1")

接著,我們以二進(jìn)制的寫方式打開一個文件,寫入圖片數(shù)據(jù):

fout = open('image.png','wb')

fout.write(cursor.fetchone()[0])

關(guān)于python中怎么向mysql中存儲圖片就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

名稱欄目:python中怎么向mysql中存儲圖片-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article30/cdocso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站標(biāo)簽優(yōu)化全網(wǎng)營銷推廣外貿(mào)網(wǎng)站建設(shè)外貿(mào)建站品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作