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
  Export data from PDF as tabular format zinho 5 647 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Webscrape from my webpage and store in database and send to grafana Armond 2 874 Jul-12-2023, 06:29 PM
Last Post: Armond
  How to detect abnormal data in big database python vanphuht91 5 1,064 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 1,120 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 1,802 Dec-12-2022, 08:22 PM
Last Post: jh67
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,277 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,306 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Reading Data from JSON tpolim008 2 1,032 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,561 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  Need Help writing data into Excel format ajitnayak87 8 2,438 Feb-04-2022, 03:00 AM
Last Post: Jeff_t

Forum Jump:

User Panel Messages

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