Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all rows from a table, throws nullpointer

Tags:

android

sqlite

I'm trying to write a function that will delete every row in a given table but I'm getting a null pointer exception. Could somebody point me in the right direction? Here is the code...

   public void deleteall(){
    SQLiteDatabase db = tweets.getWritableDatabase();
    String delete = "TRUNCATE FROM tweets";
    db.rawQuery(delete, null);

      }
like image 438
Skizit Avatar asked Jun 22 '10 15:06

Skizit


4 Answers

Check if tweets is null.

I think it's more simpler to use this call, than using rawQuery. Your rawQuery must be parsed, but using the delete method it uses already a parametrized query.

db.delete('tweets',null,null);
like image 100
Pentium10 Avatar answered Oct 28 '22 17:10

Pentium10


just to delete all rows, you can use following method.

void deleteAll()
{
    SQLiteDatabase db= this.getWritableDatabase();
    db.delete(table_name, null, null);

}
like image 30
Pattabi Raman Avatar answered Oct 28 '22 16:10

Pattabi Raman


    dataBaseHelper = new DataBaseHelper(getApplicationContext(), DataBaseHelper.DataBaseName, null, 1);
    sqLiteDatabase = dataBaseHelper.getWritableDatabase();
    if (sqLiteDatabase != null) {
        sqLiteDatabase.delete(DataBaseHelper.TableName, null, null);
        Toast.makeText(MainActivity.this, "Refresh", Toast.LENGTH_SHORT).show();
    } else
        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
    break;
}
like image 1
dmkrush Avatar answered Oct 28 '22 16:10

dmkrush


I was getting a null pointer exception, and then I realised that my method was not calling db.open() first. Added that (and db.close()) and it worked.

like image 1
Mr Chops Avatar answered Oct 28 '22 15:10

Mr Chops