Search code examples
phppythonmysqljpeg

Convert jpg images to blob and store it in a .sql file


I store jpg images into a mysql database.

I use the BLOB type to store the jpg images in the mysql database.

I use mysqldump to generate a .sql file.

I use the .sql file as backup and restore the database.

How can I convert a jpg directly to a .sql file?

I have tried this python code:

import os

import base64

archivo_destino = ".\\portada_sql\\archivo.sql"

imagen_origen = ".\\libros_nuevos_jpg\\03000\\000.jpg"

archivo = open(archivo_destino, 'w')

imageFile = open(imagen_origen, "rb") 

mi_blob = base64.b64encode(imageFile.read())

archivo.write("INSERT INTO libro_portada VALUES ('"+mi_blob+"');")

archivo.close()

But it does not work.

It is possible to create a sql file with a image stored as blob usign python?

I try to avoid:

  • upload the jpg to the server
  • import jpg to mysql
  • export mysql to .sql file

I want to generate directly the .sql file.

It is possible?

1. Magnus: I mentioned php because I can use php too, if this is possible with php.

2. Eric: From the python code mentioned above, I get:

INSERT INTO libro_portada VALUES ('b'/9j/4RivRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAE'');

From mysqldump I get:

INSERT INTO libro_portada VALUES ('ÿ\؀ÿ\ကJExif\0\0MM\0*\0\0\0\0\0\0\0\0\0\0\0\Z\0\0\0\0\0\0\0b');


Solution

  • I know is insane to store files in mysql, but I only work as a monkey code in this project.

    Finally i used MYSQL LOAD_FILE() function

    I uploaded my jpgs to the server and imported to mysql using: MYSQL LOAD_FILE()

    There are thoundsands of files to uplodad, so I created a python script to create the sql code and store it in sql file.

    # coding=utf-8
    #! python3
    # crea el comando mysql para importar los archivos .jpg a mysql
    # para cada libro crea un archivo sql
    # para cada archivo crea un comando insert
    # INSERT INTO  archivo  SET libro_fk =,orden =,nombre =,archivo =,mime_type ="image/jpeg",numero_pagina =,libro_completo =0;
    
    import os
    
    
    libros = [
    "K0LEM",
    "K3J2S"
    ]
    
    
    ids = [
    
    "2390",
    "2399"
    ]
    
    
    
    
    total = len(libros)
    
    #para cada libro...
    for i in range(total):
        #crearemos un archivo sql para cada libro
        archivo_destino = ".\\archivos_sql\\" + ids[i] + ".sql"
        archivo = open(archivo_destino, 'w')
        carpeta_a_explorar = ".\\libros_nuevos_jpg\\" + libros[i] + "\\"
        archivos = os.listdir(carpeta_a_explorar)
        #crear una lista con los archivos
        archivos.sort()
        #ordena la lista
        cuantos_archivos = len(archivos)
        #cuantos archivos
        #
        # para cada archivo encontrado se creara un insert...
        #
        for jpg_file in range(cuantos_archivos):
            archivo.write("INSERT INTO  archivo  SET libro_fk ="+ ids[i] +",orden = "+str(jpg_file)+",nombre ='"+ archivos[jpg_file] +"',archivo = LOAD_FILE('/var/lib/mysql-files/"+ libros[i] +"/"+ archivos[jpg_file] +"'),mime_type ='image/jpeg',numero_pagina = "+str(jpg_file)+",libro_completo =0;")
            archivo.write("\n")
        archivo.close()
    

    The result is a sql file with an insert command per line using MYSQL LOAD_FILE()

    INSERT INTO  archivo  SET libro_fk =2390,orden = 0,nombre ='000.jpg',archivo = LOAD_FILE('/var/lib/mysql-files/K0LEM/000.jpg'),mime_type ='image/jpeg',numero_pagina = 0,libro_completo =0;
    INSERT INTO  archivo  SET libro_fk =2390,orden = 1,nombre ='001.jpg',archivo = LOAD_FILE('/var/lib/mysql-files/K0LEM/001.jpg'),mime_type ='image/jpeg',numero_pagina = 1,libro_completo =0;
    INSERT INTO  archivo  SET libro_fk =2390,orden = 2,nombre ='002.jpg',archivo = LOAD_FILE('/var/lib/mysql-files/K0LEM/002.jpg'),mime_type ='image/jpeg',numero_pagina = 2,libro_completo =0;
    

    I store jpg files in /var/lib/mysql-files/ so mysql can import it.