I wanted to know what is the way to save images (business photos) in SQLite database. I am 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 data type can I use to store an image? and how do I insert it?
In my data constructor, it will return an image of what data type?
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 a business image or logo for each business that I am going to do, these would be approximately 100 businesses. I listen to recommendations, and it helps since I never work with images in SQLite. Thank you!!
In SQLite 3, you can define the column with affinity
BLOB
, which is an acronym for Binary Large Object and , as its name implies, its purpose is to store binary information, such as an image, in the format of your choice.The adaptation to the table creation code would then be something like this:
In java, this field can be represented as an array of bytes. The array is, by definition, 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 disk images would look like this:
With this routine in hand, the preparation of each business could be as follows:
You should be aware that a BLOB has a maximum size in SQLite, which cannot store large amounts of data in each row. More information can be found in the maximum length of a string or blob section of the Limits in SQLite document.