Reputation: 19705
I'm changing my file organization for mysqlite files. Before, I had all methods for all tables in 1 file : DatabaseHandler
The file looks like that :
public class DatabaseHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "mybase.db";
private static final String TABLE_TIENDA = "tienda";
private static final String TABLE_PROYECTO = "proyecto";
// Columns Name
private static final String TIENDA_ID = "id";
private static final String TIENDA_CANAL = "canal";
private static final String TIENDA_CIUDAD = "ciudad";
...
private static String CREATE_PDV_PROYECTO = "CREATE TABLE " + TABLE_PROYECTO + "(" + PROYECTO_ID
+ " INTEGER PRIMARY KEY," + PROYECTO_PLAN_ID + " INTEGER," + PROYECTO_DESC + " TEXT," + PROYECTO_VIGENCIA
+ " TEXT," + PROYECTO_ACTIVO + " INTEGER)";
...
SQLiteDatabase db = null;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getReadableDatabase();
}
public void addProyecto(Proyecto p) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PROYECTO_ID, p.proyectoid);
values.put(PROYECTO_PLAN_ID, p.planid);
values.put(PROYECTO_DESC, p.proyectodsc);
values.put(PROYECTO_VIGENCIA, p.vigencia);
values.put(PROYECTO_ACTIVO, p.activo);
// Inserting Row
db.insert(TABLE_PROYECTO, null, values);
Log.e("DB", "inserting project:" + p.proyectodsc);
db.close(); // Closing database connection
}
The thing is I work in several tables : Tienda, Proyecto, Captura and more. The Java Object Tinda has a Proyecto Object inside.
The thing is I wanted to separate each Table in a separated file. So I create clases DatabaseHandler, TiendaHandler, ProyectoHandler and CapturaHandler.
The thing in TiendaHandler is I use ProyectoHandler references which produce the error in the title :
public void addAllTienda(ArrayList<Pdv> ltiendas) {
SQLiteDatabase db = this.getWritableDatabase();
for (Pdv tienda : ltiendas) {
addTienda(tienda);
Proyecto p = tienda.getProyecto();
int proyectoId = p.proyectoid;
if (!ProyectoHandler.existProyecto(db, proyectoId)) { // THis line produce error
ProyectoHandler.addProyecto(db, p);
}
}
db.close();
}
I don't know how to do it? It is good practice what I'm doing???
Upvotes: 0
Views: 110
Reputation: 14847
If you use db.close()
then you can't use this object again.
You should get a new reference to the DB using again getWritableDatabase()
.
A good thing to do: Don't save a reference to the Database, everytime you need to use it use open it and when you are done close it. Don't keep a reference alive more than it needs.
I refer to this
SQLiteDatabase db = null;
in DatabaseHandler
In your addProyecto
you create a new reference to getWritableDatabase
so the reference created in DatabaseHandler
is losed. Since you close the reference inside addProyecto
(with db.close
) when you use the object db
in other methods of DatabaseHandler
you will get this Exception
.
A way to fix it is to remove this
SQLiteDatabase db = null;
from the class and open/close the database in every method.
Upvotes: 1