Save images in SQLite? [closed]

2

I wanted to know how to save images (business photos) in the SQLite database. I'm with an app that shows all the businesses in the city where I live.

This is the code that creates the table:

  public static final String TABLE_CREATE =               //METODO PARA CREAR TABLA
            "CREATE TABLE " + TABLE_NEGOCIOS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_NOMBRE + " TEXT, " +
                    COLUMN_DIRECCION + " TEXT, " +
                    COLUMN_DESCRIPCION + " TEXT, " +
                    COLUMN_CATEGORIA + " TEXT " +
                    ")";

What type of data can I use to store an image? and how do I insert it?

In my data constructor, will you return an image of what kind of data?

This is my code:

public class Negocio implements Serializable {
    long id;
    String nombre;
    String descripcion;
    String domicilio;
    String categoria;





    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getDomicilio() {
        return domicilio;
    }

    public void setDomicilio(String domicilio) {
        this.domicilio = domicilio;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public String toString() { return nombre ; }
}

To insert the data I do it manually:

private void createdata(){
        Negocio negocio = new Negocio();
        negocio.setNombre("La buena atencion");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Carniceria, los mejores cortes");
        negocio.setCategoria("carniceria");
        dataSource.create(negocio);

        negocio = new Negocio();
        negocio.setNombre("Citadella");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Panaderia del centro con buenas masas");
        negocio.setCategoria("panaderia");
        dataSource.create(negocio);


        negocio = new Negocio();
        negocio.setNombre("La union");
        negocio.setDomicilio("H Yrigoyen");
        negocio.setDescripcion("Panaderia que abre poco dias");
        negocio.setCategoria("panaderia");
        dataSource.create(negocio);

        negocio = new Negocio();
        negocio.setNombre("Electronica John");
        negocio.setDomicilio("Alem");
        negocio.setDescripcion("De todo");
        negocio.setCategoria("electronica");
        dataSource.create(negocio);

        negocio = new Negocio();
        negocio.setNombre("La maga");
        negocio.setDomicilio("Roca");
        negocio.setDescripcion("comidas rapidas");
        negocio.setCategoria("Casa de comida");
        dataSource.create(negocio);
    }

I wanted to add an image or business logo for each business I'm going to do, these would be about 100 businesses. I listen to recommendations, and it helps since I never work with images in SQLite. Thanks !!

    
asked by Ibarra Emiliano 17.10.2018 в 18:12
source

1 answer

2

In SQLite 3, you can define the column with affinity BLOB , which is an acronym for B inary L arge Ob ject and, as the name implies, its purpose is to store binary information, such as an image in the format of your choice.

The adaptation to the creation code of the table would be, then, something like this:

public static final String TABLE_CREATE =               //METODO PARA CREAR TABLA
        "CREATE TABLE " + TABLE_NEGOCIOS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NOMBRE + " TEXT, " +
                COLUMN_DIRECCION + " TEXT, " +
                COLUMN_DESCRIPCION + " TEXT, " +
                COLUMN_CATEGORIA + " TEXT " +
                COLUMN_IMAGEN + " BLOB " +
                ")";

In java, this field can be represented as a byte array. The arrangement is, by definition, of variable length. You must then add a member to your class that represents a business, so that it contains an array of bytes that is the image.

An example, adapted from the SQLite tutorial , to read the images of the disk, it would look like this:

private byte[] readFile(String file) {
        ByteArrayOutputStream bos = null;
        try {
            File f = new File(file);
            FileInputStream fis = new FileInputStream(f);
            byte[] buffer = new byte[1024];
            bos = new ByteArrayOutputStream();
            for (int len; (len = fis.read(buffer)) != -1;) {
                bos.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            System.err.println(e.getMessage());
        } catch (IOException e2) {
            System.err.println(e2.getMessage());
        }
        return bos != null ? bos.toByteArray() : null;
    }

With this routine in hand, the preparation of each business could be as follows:

    negocio = new Negocio();
    negocio.setNombre("Electronica John");
    negocio.setDomicilio("Alem");
    negocio.setDescripcion("De todo");
    negocio.setCategoria("electronica");
    negocio.setImagen(readFile("/home/juan/imagenes/logoJohn.png"));
    dataSource.create(negocio);

You must bear in mind that a BLOB has a maximum size in SQLite, which can not store large amounts of data in each row. You will find more information in the maximum length of a string or blob section of the Limits in SQLite document >

    
answered by 17.10.2018 / 18:45
source