本文目錄一覽:
- 1、python如何保存從oracle數據庫中讀取的BLOB文件
- 2、python往mysql的blob字段寫入二進制數據,怎麼做
- 3、怎麼用Python腳本怎麼從oracle數據庫中取出clob數據
python如何保存從oracle數據庫中讀取的BLOB文件
import cx_Oracle
con = cx_Oracle.connect(『username』, 『password』, 『dsn』)
blob_sql = “select column_name from table where clause”
cursor = con.cursor()
cursor.execute(blob_sql)
result = cursor.fetchall()
file = open(‘file_name’, “wb”)
file.write(result[0][0].read()) #可以print查看result的內容,根據實際情況read
file.close()
python往mysql的blob字段寫入二進制數據,怎麼做
這有什麼難的嗎?
你把你的二進制數據可以轉成文本串插入,就跟普通的插入一樣啊。
import MySQLdb, cPickle
# Connect to a DB, e.g., the test DB on your localhost, and get a cursor
connection = MySQLdb.connect(db=”test”)
cursor = connection.cursor( )
# Make a new table for experimentation
cursor.execute(“CREATE TABLE justatest (name TEXT, ablob BLOB)”)
try:
# Prepare some BLOBs to insert in the table
names = ‘aramis’, ‘athos’, ‘porthos’
data = { }
for name in names:
datum = list(name)
datum.sort( )
data[name] = cPickle.dumps(datum, 2)
# Perform the insertions
sql = “INSERT INTO justatest VALUES(%s, %s)”
for name in names:
cursor.execute(sql, (name, MySQLdb.escape_string(data[name])) )
# Recover the data so you can check back
sql = “SELECT name, ablob FROM justatest ORDER BY name”
cursor.execute(sql)
for name, blob in cursor.fetchall( ):
print name, cPickle.loads(blob), cPickle.loads(data[name])
finally:
# Done. Remove the table and close the connection.
cursor.execute(“DROP TABLE justatest”)
connection.close( )
怎麼用Python腳本怎麼從oracle數據庫中取出clob數據
stmt = con.prepareStatement(“select attach,fjmc,piid,swsj from receiveFile ”);//attach是clolb對象
rs = stmt.executeQuery( );
while (rs.next()) {
java.sql.Blob blob = rs.getBlob(1);//這一句可獲得blob,clob等對象。
然後再把blob轉成文件
File file = new File(“G:\\XiangMu_dwoa\\數據庫文件資料\\aaa”);
OutputStream fout = new FileOutputStream(file);
//下面將BLOB數據寫入文件
byte[] b = new byte[1024];
int len = 0;
while ( (len = ins.read(b)) != -1) {
fout.write(b, 0, len);
你可以參考一下
原創文章,作者:TCMPN,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/313639.html