Tengo una clase BD
padre:
public class DB extends SQLiteOpenHelper {
private static final int VERSION_BASEDATOS = 1;
private static final String NOMBRE_BASEDATOS = "DB.db";
private Context context;
public DataExpoAuto(Context context) {
super(context, NOMBRE_BASEDATOS, null, VERSION_BASEDATOS);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase
db.execSQL(TFavorito.create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
db.execSQL("DROP TABLE IF EXISTS " + TFavorito.TAG);
onCreate(db);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
Luego para crear tablas separadas hago herencia:
public class TFavorito extends DB{
public static final String TAG = TFavorito.class.getSimpleName().toLowerCase();
public static final String[] array={"id","titulo","descripcion","tipo"};
public static final String create= "CREATE TABLE "+TAG+" ( "+array[0]+" INTEGER, "
+array[1]+" TEXT NOT NULL, "+array[2]+" TEXT NOT NULL, "+array[3]+" INTEGER );";
private static TFavorito INSTANCE = null;
// creador sincronizado para protegerse de posibles problemas multi-hilo
// otra prueba para evitar instanciación múltiple
private synchronized static void createInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new TFavorito(context);
}
}
public static TFavorito getInstance(Context context) {
if (INSTANCE == null) createInstance(context);
return INSTANCE;
}
public TFavorito(Context context) {
super(context);
}
public void insertar(Favorito favorito){
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.insert(TAG, null, favorito.getContentValues(array));
Log.d(TAG, "Inserto correctamente "+TAG);
}
db.close();
}
public Favorito leer(int id,int tipo) {
Favorito favorito = null;
String selection = array[0]+" = ? AND "+array[3]+" = ?";
// Specify arguments in placeholder order.
String[] selectionArgs = {String.valueOf(id),String.valueOf(tipo)};
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TAG, array, selection, selectionArgs, null, null, null, null);
if((c != null)&& c.moveToFirst()) {
favorito = new Favorito();
favorito.parceObaject(c);
Log.d(TAG, "Recupero correctamente "+TAG);
c.close();
}
db.close();
return favorito;
}
Inserta bien los datos y recupera bien, pero cuando consulto datos que no hay en la tabla sale el error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developerdj.expoauto/com.developerdj.expoauto.vista.activity.ConcesionariaActiviy}: android.database.sqlite.SQLiteException: no such table: tfavorito (code 1): , while compiling: SELECT id, titulo, descripcion, tipo FROM tfavorito WHERE id = ? AND tipo = ?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: tfavorito (code 1): , while compiling: SELECT id, titulo, descripcion, tipo FROM tfavorito WHERE id = ? AND tipo = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
at com.developerdj.expoauto.data.TFavorito.leer(TFavorito.java:65)