Python Forum
Reading blob data from database by python and store it in .zip format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading blob data from database by python and store it in .zip format
#1
Hi,
I am trying to store blob data from my oracle database using python and trying to store it in a local folder in .zip format. So i have two codes now one of which is running fine but fetching only one row and storing it as .zip in a folder, but the other one in which i am trying to fetch multiple row , not working , Probably i am doing some basic mistake, but not able to figure out , so please help me . Below are the codes:-
code 1-running fine and fetching only one row and storing it as .zip in a folder:-
import cx_Oracle
import numpy as np
import pandas as pd
con = cx_Oracle.connect("OPTDB1", "OPTDB1", "INDLIN2112:1524/OPTEPC1")
cursor = con.cursor()
sql_string = """SELECT F_name, F_released_data FROM epc_project where rownum<5"""
cursor.execute(sql_string)
#Path="C:\Users\adityasi\Desktop\MY Important\QueryGeneratorPytthonFinal\Project\EPCPROJECTS"
row = cursor.fetchone()
blobdata = np.array(row[1].read())
cursor.close()
filename = str(row[0]) + ".zip"
f = open(filename, 'w+b')
binary_format = bytearray(blobdata)
f.write(binary_format)
f.close()
code 2-Throwing error and unable to fetch multiple row and storing it as .zip in a folder:-
import cx_Oracle
import numpy as np
import pandas as pd
con = cx_Oracle.connect("OPT", "OPT", "INDLIN2338:1521/OPT1")
cursor = con.cursor()
sql_string = """SELECT F_name, F_released_data FROM epc_project where rownum<5"""
cursor.execute(sql_string)
rows = cursor.fetchall()
for row in rows:
        blobdata= np.array(row[1].read())
        cursor.close()
        filename =str(row[0]) + ".zip"
con.close()
f = open(filename, "wb")
binary_format = bytearray(blobdata)
f.write(binary_format)
f.close()
Error:-

Error:
blobdata= np.array(row[1].read()) AttributeError: 'str' object has no attribute 'read' Process finished with exit code 1
Please help me out here.
Thanks, Aditya
Reply
#2
It seems like you are not getting the same type of object during both procedures.

In your code 1 try:
row = cursor.fetchone()
print(type(row))
blobdata = np.array(row[1].read())
cursor.close()
And in your code 2:
for row in rows:
    print(type(row))
To compare them and see if you are obtaining the same. Also, you are closing the cursor constantly during the loop. That does not look right
Reply
#3
In your "code 2" the "cursor.close()" is in a strange place. It should not be in the for loop.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,595 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Python script to extract data from API to database melpys 0 1,159 Aug-12-2024, 05:53 PM
Last Post: melpys
  Reading an ASCII text file and parsing data... oradba4u 2 1,620 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Export data from PDF as tabular format zinho 5 2,738 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Webscrape from my webpage and store in database and send to grafana Armond 2 1,956 Jul-12-2023, 06:29 PM
Last Post: Armond
  How to detect abnormal data in big database python vanphuht91 5 2,599 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 2,315 May-22-2023, 03:25 AM
Last Post: Calab
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 4,171 Dec-12-2022, 08:22 PM
Last Post: jh67
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 3,442 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 3,418 Nov-08-2022, 07:16 PM
Last Post: marlonbown

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020