python如何保存从oracle数据库中读取的BLOB文件

Python014

python如何保存从oracle数据库中读取的BLOB文件,第1张

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高难度代码例子、Python最复杂代码例子

张三讲法

TA获得超过3912个赞

关注

成为第46位粉丝

#IT教育# #IT# #程序员# #人工智能#

最近学习pytorch,看到下面的Python高难度代码例子和Python最复杂代码例子:

from google.colab import output as colab_output

from base64 import b64decode

from io import BytesIO

from pydub import AudioSegment

RECORD = """

const sleep = time =>new Promise(resolve =>setTimeout(resolve, time))

const b2text = blob =>new Promise(resolve =>{

const reader = new FileReader()

reader.onloadend = e =>resolve(e.srcElement.result)

reader.readAsDataURL(blob)

})

var record = time =>new Promise(async resolve =>{

stream = await navigator.mediaDevices.getUserMedia({ audio: true })

recorder = new MediaRecorder(stream)

chunks = []

recorder.ondataavailable = e =>chunks.push(e.data)

recorder.start()

await sleep(time)

recorder.onstop = async ()=>{

blob = new Blob(chunks)

text = await b2text(blob)

resolve(text)

}

recorder.stop()

})

"""

def record(seconds=1):

display(ipd.Javascript(RECORD))

print(f"Recording started for {seconds} seconds.")

s = colab_output.eval_js("record(%d)" % (seconds * 1000))

print("Recording ended.")

b = b64decode(s.split(",")[1])

fileformat = "wav"

filename = f"_audio.{fileformat}"

AudioSegment.from_file(BytesIO(b)).export(filename, format=fileformat)

return torchaudio.load(filename)

waveform, sample_rate = record()

print(f"Predicted: {predict(waveform)}.")

ipd.Audio(waveform.numpy(), rate=sample_rate)

js 的Promise函数对象编程,字符串javascript函数对象,IPython解释js对象,解释结果和python代码结合,IPython Shell显示非字符串数据,python音频使用IPython简单调用。

复杂Python模块下的多知识点结合代码,是Python高难度代码的体现。

Js的Promise理解为动态函数,比C++的类成员函数和全局函数这类静态形式的函数处理灵活,不过初学者理解起来麻烦。代码里sleep和b2text都代表一些处理函数,也就是几行代码,而不是数据。通常来讲,变量一般代表数据,但是这里代表了指令。

这有什么难的吗?

你把你的二进制数据可以转成文本串插入,就跟普通的插入一样啊。

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( )