OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

python blob extractor from a sql query not working properly

  • Thread starter Thread starter nando-ando
  • Start date Start date
N

nando-ando

Guest
im making a blob extractor to get some images from a db with a query for the following rows: rows selected from query

so im using the following code to extract said images:

Code:
import psycopg2
import base64

# Conectar a la base de datos
conn = psycopg2.connect(user="serverinfo") 

cursor = conn.cursor()
# Ejecutar la consulta
cursor.execute('Select "id", "name", "flip_image" from product_template where flip_image is not null')
rows = cursor.fetchall()

for row in rows:
        product_id = row[0]
        product_name = row[1]
        image_data = row[2]
                
        if image_data:
                # Decodificar la imagen base64
                image_bytes = base64.b64decode(image_data)
                
                #Guardar la imagen en un archivo
                with open(f"{product_id}.png", 'wb') as image_file:
                        image_file.write(image_bytes)
                print(f"imagen {product_name} (ID: {product_id})guardada.")
                
cursor.close()
conn.close()

as it is right now the code is working and i get all the image files as long as i have

with open(f"{product_id}.png", 'wb') as image_file:

but when i try to change the product_id to product_name to print all the pngs with the actual name of the image instead of an id using

with open(f"{product_name}.png", 'wb') as image_file:

or

with open(f"{product_id}_{product_name}.png", 'wb') as image_file:

i get a no file error:

FileNotFoundError: [Errno 2] No such file or directory: 'Caja Registradora Casio Pcr-t280 / 1200 Items Con Nombre.png'

Why would the extraction work only with the product id variable and not the product name variable?
<p>im making a blob extractor to get some images from a db with a query for the following rows:
<a href="https://i.sstatic.net/BAum7Dzu.png" rel="nofollow noreferrer">rows selected from query</a></p>
<p>so im using the following code to extract said images:</p>
<pre><code>import psycopg2
import base64

# Conectar a la base de datos
conn = psycopg2.connect(user="serverinfo")

cursor = conn.cursor()
# Ejecutar la consulta
cursor.execute('Select "id", "name", "flip_image" from product_template where flip_image is not null')
rows = cursor.fetchall()

for row in rows:
product_id = row[0]
product_name = row[1]
image_data = row[2]

if image_data:
# Decodificar la imagen base64
image_bytes = base64.b64decode(image_data)

#Guardar la imagen en un archivo
with open(f"{product_id}.png", 'wb') as image_file:
image_file.write(image_bytes)
print(f"imagen {product_name} (ID: {product_id})guardada.")

cursor.close()
conn.close()
</code></pre>
<p>as it is right now the code is working and i get all the image files as long as i have</p>
<p><code>with open(f"{product_id}.png", 'wb') as image_file:</code></p>
<p>but when i try to change the product_id to product_name to print all the pngs with the actual name of the image instead of an id using</p>
<p><code>with open(f"{product_name}.png", 'wb') as image_file:</code></p>
<p>or</p>
<p><code>with open(f"{product_id}_{product_name}.png", 'wb') as image_file:</code></p>
<p>i get a no file error:</p>
<p><code>FileNotFoundError: [Errno 2] No such file or directory: 'Caja Registradora Casio Pcr-t280 / 1200 Items Con Nombre.png'</code></p>
<p>Why would the extraction work only with the product id variable and not the product name variable?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top