本文目录一览:
- 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);
你可以参考一下