删除表中的所有行,抛出nullpointer

时间:2023-01-20 19:19:25

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);

      }

7 个解决方案

#1


57  

Check if tweets is null.

检查tweet是否为空。

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.

我认为使用这个调用比使用rawQuery更简单。必须解析rawQuery,但使用delete方法时,它已经使用了一个参数化查询。

db.delete('tweets',null,null);

#2


11  

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

为了删除所有的行,您可以使用以下方法。

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

}

#3


1  

    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;
}

#4


1  

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.

我得到一个空指针异常,然后我意识到我的方法不是首先调用db.open()。添加了那个(和db.close())),它就可以工作了。

#5


0  

SQLite does not have any TRUNCATE statement, so you must do:

SQLite没有任何截断语句,所以必须:

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

#6


0  

Setting the second parameter to null deletes all rows.

将第二个参数设置为null将删除所有行。

public int deleteAllRowsInTable() {

    SQLiteDatabase db = helper.getWritableDatabase(); // helper = MyDatabaseHelper
    String whereClause = null; // delete all rows
    String[] whereArgs = { null };
    int count = db.delete(MyDatabaseHelper.TABLE_NAME, whereClause, whereArgs);
    db.close(); // don't forget to close the database
    return count; // number of rows affected
}

The above code was for illustration. This can just be simplified to

上面的代码是用来说明的。这个可以简化成

public int deleteAllRowsInTable() {

    SQLiteDatabase db = helper.getWritableDatabase();
    int count = db.delete(MyDatabaseHelper.TABLE_NAME, null, null);
    db.close();
    return count;
}

#7


0  

The right answer is this:

正确的答案是:

db.delete('tweets',"1",null);

From Android official documentation: SQLiteDatabase

Android官方文档:SQLiteDatabase。

#1


57  

Check if tweets is null.

检查tweet是否为空。

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.

我认为使用这个调用比使用rawQuery更简单。必须解析rawQuery,但使用delete方法时,它已经使用了一个参数化查询。

db.delete('tweets',null,null);

#2


11  

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

为了删除所有的行,您可以使用以下方法。

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

}

#3


1  

    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;
}

#4


1  

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.

我得到一个空指针异常,然后我意识到我的方法不是首先调用db.open()。添加了那个(和db.close())),它就可以工作了。

#5


0  

SQLite does not have any TRUNCATE statement, so you must do:

SQLite没有任何截断语句,所以必须:

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

#6


0  

Setting the second parameter to null deletes all rows.

将第二个参数设置为null将删除所有行。

public int deleteAllRowsInTable() {

    SQLiteDatabase db = helper.getWritableDatabase(); // helper = MyDatabaseHelper
    String whereClause = null; // delete all rows
    String[] whereArgs = { null };
    int count = db.delete(MyDatabaseHelper.TABLE_NAME, whereClause, whereArgs);
    db.close(); // don't forget to close the database
    return count; // number of rows affected
}

The above code was for illustration. This can just be simplified to

上面的代码是用来说明的。这个可以简化成

public int deleteAllRowsInTable() {

    SQLiteDatabase db = helper.getWritableDatabase();
    int count = db.delete(MyDatabaseHelper.TABLE_NAME, null, null);
    db.close();
    return count;
}

#7


0  

The right answer is this:

正确的答案是:

db.delete('tweets',"1",null);

From Android official documentation: SQLiteDatabase

Android官方文档:SQLiteDatabase。