Error in SQLiteDatabase

1

I'm trying to delete data from a table, but every time I open the application and I give the delete button it stops and I get out of the application I think it's in SQLiteDatabase db= this.getWritableDatabase();

public class DbHelper extends SQLiteOpenHelper {
public static final String TAG = DbHelper.class.getSimpleName();
public static final String DB_NAME = "myapp.db";
public static final int DB_VERSION = 1;

public static final String USER_TABLE = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_usurarios = "usurarios";
public static final String COLUMN_PASS = "password";

/*
create table users(
    id integer primary key autoincrement,
    usurarios text,
    password text);
 */
public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_usurarios + " TEXT,"
        + COLUMN_PASS + " TEXT);";

public DbHelper(Context applicationContext, Object o, Context context, int i) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_USERS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String usurarios, String password) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_usurarios, usurarios);
    values.put(COLUMN_PASS, password);

    long id = db.insert(USER_TABLE, null, values);
    db.close();

    Log.d(TAG, "Usuario insertado" + id);
}





public boolean getUser(String usurarios, String pass){
    //HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "select * from  " + USER_TABLE + " where " +
            COLUMN_usurarios + " = " + "'"+usurarios+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {

        return true;
    }
    cursor.close();
    db.close();

    return false;
}

public String eliminar(String usuarios){
    String mensaje="";

    SQLiteDatabase db= this.getWritableDatabase();

    int cantidad = db.delete("myapp.db","usuarios='"+usuarios+"'",null);

    if(cantidad!=0){
        mensaje="eliminado correctamente";
    }else{
        mensaje="No existe";
    }

    return mensaje;
}
public ArrayList llenar_lv(){
    ArrayList<String> lista = new ArrayList<>();
    SQLiteDatabase database = this.getWritableDatabase();
    String q = "SELECT * FROM datos";
    Cursor registros = database.rawQuery(q,null);
    if(registros.moveToFirst()){
        do{
            lista.add(registros.getString(1));
        }while(registros.moveToNext());
    }
    return lista;

}

}

This is the method to remove

public String eliminar(String usuarios){
    String mensaje="";

    SQLiteDatabase db= this.getWritableDatabase();

    int cantidad = db.delete("myapp.db","usuarios='"+usuarios+"'",null);

    if(cantidad!=0){
        mensaje="eliminado correctamente";
    }else{
        mensaje="No existe";
    }

    return mensaje;
}

LogCat errors

11-24 01:49:21.333 6797-6797/com.techobbyist.signuplogin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       java.lang.NullPointerException
                                                                           at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                           at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                           at com.techobbyist.signuplogin.DbHelper.eliminar(DbHelper.java:94)
                                                                           at com.techobbyist.signuplogin.RegisterActivity$1.onClick(RegisterActivity.java:47)
                                                                           at android.view.View.performClick(View.java:4475)
                                                                           at android.view.View$PerformClick.run(View.java:18786)
                                                                           at android.os.Handler.handleCallback(Handler.java:730)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                           at android.os.Looper.loop(Looper.java:176)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:525)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                           at dalvik.system.NativeStart.main(Native Method)
    
asked by zzxbx 24.11.2017 в 01:08
source

2 answers

1

This error:

at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)

Appears when you are passing a null context to your constructor.

and for:

at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)

It's because you use getWritableDatabase() more than once without closing the previous session of the BD.

To close the BD session you must use db.close(); in the delete function, after performing the action delete()

public String eliminar(String usuarios){
    String mensaje="";

    SQLiteDatabase db= this.getWritableDatabase();

    int cantidad = db.delete("myapp.db","usuarios='"+usuarios+"'",null);

    if(cantidad!=0){
        mensaje="eliminado correctamente";
    }else{
        mensaje="No existe";
    }
    db.close(); // esto va aqui
    return mensaje;
}

And on your button you must instantiate the bd and call the function delete ()

private Datahelper mydb; //intancia

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    mydb =new Datahelper(getActivity()); //contexto
    
answered by 24.11.2017 / 07:52
source
0

Remember that the method to remove delete () requires as the first parameter, the table in which to perform the deletion of the data.

public int delete(String tabla, String whereClause, String[] whereArgs) {

you are defining the name of the database which is incorrect:

int cantidad = db.delete("myapp.db","usuarios='"+usuarios+"'",null);

You must define the name of the table , not the one in the database, also check the documentation to define correctly how to delete:

int cantidad = db.delete(USER_TABLE,"usuarios = ?", new String[]{usuarios});
    
answered by 24.11.2017 в 01:15