The method onCreate isn't calling when I make a new reference a class SQLite. The problem is that the new table isn't created and when i do a query a error appears to indicate that the table isn't exists.
I tried call to method onCreate from Constructor but this is calling all time when I make a new instance so it isn't the solution.
My code is:
public class FotografiaDAOSqlite extends SQLiteOpenHelper {
//Sentencia SQL para crear la tabla de Usuarios
String sqlCreate = "CREATE TABLE fotografia (ID_FOTOGRAFIA INT PRIMARY KEY, ID_ARTICULO INT, URL VARCHAR(1000) );";
public FotografiaDAOSqlite(Context contexto, String nombre,
CursorFactory factory, int version) {
super(contexto, nombre, factory, version);
//onCreate(this.getReadableDatabase());
}
public void onCreate(SQLiteDatabase db) {
//Se ejecuta la sentencia SQL de creación de la tabla
db.execSQL(sqlCreate);
}
}
called from here:
ArticulosDAOSqlite accesoArticulos = new ArticulosDAOSqlite(getBaseContext(), "DBAuchic18", null, 1);
FotografiaDAOSqlite fotografiaDAO = new FotografiaDAOSqlite(getBaseContext(), "DBAuchic18", null, 1);
SQLiteDatabase dbb = fotografiaDAO.getWritableDatabase();
_art = fotografiaDAO.obtenerFotografias(dbb, b.getInt("idarticulo"), new Articulos());
public Articulos obtenerFotografias(SQLiteDatabase db,int idArticulo, Articulos arti){
ArrayList<String> arts = new ArrayList<String>();
String[] args = new String[] {String.valueOf(idArticulo)};
Cursor c = db.rawQuery("SELECT url FROM fotografia WHERE ID_ARTICULO=?",args);
arti.setId_articulo(idArticulo);
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
arti.getListaFotografias().add(c.getString(0));
} while(c.moveToNext());
}
c.close();
return arti;
}
public Articulos obtenerFotografias(SQLiteDatabase db,int idArticulo, Articulos arti){
ArrayList<String> arts = new ArrayList<String>();
String[] args = new String[] {String.valueOf(idArticulo)};
Cursor c = db.rawQuery("SELECT url FROM fotografia WHERE ID_ARTICULO=?",args);
arti.setId_articulo(idArticulo);
if (c.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
arti.getListaFotografias().add(c.getString(0));
} while(c.moveToNext());
}
c.close();
return arti;
}
The method is called:
ArticulosDAOSqlite accesoArticulos = new ArticulosDAOSqlite(getBaseContext(), "DBAuchic18", null, 1);
FotografiaDAOSqlite fotografiaDAO = new FotografiaDAOSqlite(getBaseContext(), "DBAuchic18", null, 1);
SQLiteDatabase dbb = fotografiaDAO.getWritableDatabase();
_art = fotografiaDAO.obtenerFotografias(dbb, b.getInt("idarticulo"), new Articulos());