1.熟悉、梳理、总结下Oracle相关知识体系
2.欢迎批评指正,跪谢一键三连!
- 资源下载: oci.dll、oraocci11.dll、oraociei11.dll3个资源文件
- 资源下载: Instant Client Setup.exe
- 资源下载: oci.dll、oraocci11.dll、oraociei11.dll3个资源文件
- 资源下载: Instant Client Setup.exe
- 文章参考:【kettle005】kettle访问Oracle数据库并处理数据至execl文件(已更新)
- 文章参考:【oracle】cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number,ORA-00911,DPI-1047
- 文章参考:【oracle】cx_Oracle.DatabaseError: ORA-24816: Expanded non LONG bind data supplied after actual LONG o
文章目录
- 1.`oracle`建表语句
- 2.图片转为字节、base64编码等形式插入`oracle`数据库
- 3.`oracle`数据库存储、处理效果
1.oracle
建表语句
-
create table youli.youli_image_test (id varchar(16), image blob, imagebase64 clob, imagebin blob ) select * from youli.youli_image_test WHERE rownum<4SELECT dbms_lob.substr(IMAGEBIN, 40000, 1) ff FROM youli.youli_image_test
2.图片转为字节、base64编码等形式插入oracle
数据库
-
图片数据转码、base64转码,反转可视化显示验证
-
python
代码实现import sys import base64 import cx_Oracle from PIL import Image import os import iopath = "D:\桌面\itest" def intoimg2orcl( id_cnt, img , base64img, bytesimg ):database = cx_Oracle.connect('system/oracle@192.168.48.150/xe') # 建立一个Oracle连接# cx_Oracle.connect("用户名/密码@Oracle服务器IP/Oracle的SERVICE_NAME")cursor = database.cursor() # 存入图片, 创建游标sql = "insert into youli.youli_image_test values (:1, :2, :3, :4)"args = ( str(id_cnt), img , base64img, bytesimg )cursor.execute( sql, args )database.commit()cursor.close() # 关闭游标database.close() # 关闭数据库连接print("===============")print("Done! ")def selectimgforcl():database = cx_Oracle.connect('system/oracle@192.168.48.150/xe') # 建立一个Oracle连接# cx_Oracle.connect("用户名/密码@Oracle服务器IP/Oracle的SERVICE_NAME")cursor = database.cursor() # 存入图片, 创建游标sql = "select * from youli.youli_image_test WHERE rownum<4"cursor.execute( sql )rows = cursor.fetchall()for row in rows:id = row[0]image = row[1].read()imagebase64 = row[2].read()imagebin = row[3].read()bimg = byte2image(imagebin) # 图片字节数据可视化显示验证bimg.show()base642img( imagebase64 ) # 图片base64数据可视化显示验证breakcursor.close() # 关闭游标database.close() # 关闭数据库连接 def encode_image(file_path):"""读取图片文件并转换为base64编码"""with open(file_path, 'rb') as image_file:encoded_string = base64.b64encode(image_file.read())# b64_encode = 'data:image/jpeg;base64,%s' % sreturn encoded_string.decode('utf-8') def read2byte( jpg_fpath ):"""图片数据转为字节数据"""image = Image.open(jpg_fpath)img_bytes = io.BytesIO() # 创建一个字节流管道image = image.convert("RGB") # 把PNG格式转换成的四通道转成RGB的三通道,然后再保存成jpg格式image.save(img_bytes, format="JPEG") # 将图片数据存入字节流管道, format可以按照具体文件的格式填写image_bytes = img_bytes.getvalue() # 从字节流管道中获取二进制return image_bytes def byte2image(byte_data):''' byte转为图片, byte_data: 二进制 '''image = Image.open(io.BytesIO(byte_data))return image def base642img( base64_encod_str ):"""base64数据转为图片数据可视化"""res = base64_encod_strimg_b64decode = base64.b64decode(res)image = io.BytesIO(img_b64decode)img = Image.open(image)img.show()"""循环处理图片数据,入库,查询、可视验证""" id_cnt = 1 for jpg in os.listdir(path):jpg_fpath = path + '\\' + jpgimg_fp = open( jpg_fpath, 'rb')img = img_fp.read()img_base64 = encode_image( jpg_fpath )img_bytes = read2byte( jpg_fpath )print( len(img), len(img_base64), len(img_bytes) )intoimg2orcl( id_cnt, img , img_base64, img_bytes) # 插入不同类型图片编码数据至oracle数据库img_fp.close()id_cnt += 1selectimgforcl()break
3.oracle
数据库存储、处理效果
open( jpg_fpath, 'rb').read(),image
base64,base64.b64encode(image_file.read())