Unable to delete Sqlite table

1

I want to make sure to delete a table (if it exists) before creating another table with the same name. But this does not happen, I realize because I make changes in the code and command to compile and execute, and I only notice the changes (which tells me that the new table was created) when I uninstall the application previously.

package net.eqsoft.pacienteapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class PacienteSQLiteHelper extends SQLiteOpenHelper {
   String sqlDrop = "drop table if exists registro";
   String sqlCreate = "create table registro (reg integer, familiar text)";
   String sqlIniciar = "insert into registro (reg, familiar) values (2, 'none')";

   public PacienteSQLiteHelper(Context context, String nombre, CursorFactory factory, int version) {
        super(context, nombre, factory, version);
   }

   @Override
   public void onCreate(SQLiteDatabase pacientedb) {
       pacientedb.execSQL(sqlDrop);
       pacientedb.execSQL(sqlCreate);
       pacientedb.execSQL(sqlIniciar);
   }

   @Override
   public void onUpgrade(SQLiteDatabase pacientedb, int versionAnterior, int versionNueva) {
       pacientedb.execSQL("drop table if exists registro");
       onCreate(pacientedb);
   }
}
    
asked by manduinca 28.05.2016 в 21:26
source

2 answers

1

Well I found the solution after taking a few turns, this problem is sure to be common. Every time I compiled and executed the application, there was already a database created with the same name as the one I wanted to create, therefore none of the orders: Drop, Create, Insert; later they were processed.

The solution is to delete the database at the time of installation of the application, just: context.deletedatabase ("namedatabase").

    
answered by 29.05.2016 / 00:05
source
0

After the first occasion, you will always have a bd created until you run the onUpgrade () method, so you can call this method when you install a new application:

 @Override
   public void onUpgrade(SQLiteDatabase pacientedb, int versionAnterior, int versionNueva) {
       pacientedb.execSQL("drop table if exists registro");
       onCreate(pacientedb);
   }

You can use context.deletedatabase() although the application should use the previous method.

I think that what you want is related to tests and what is generally done is to go to the application and delete the data or the cache, thereby ensuring you delete the database.

    
answered by 29.05.2016 в 01:17