Android 从表中删除所有行,抛出空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3094444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete all rows from a table, throws nullpointer
提问by Skizit
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);
}
回答by Pentium10
Check if tweets
is null.
检查是否tweets
为空。
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);
回答by Pattabi Raman
just to delete all rows, you can use following method.
只是要删除所有行,您可以使用以下方法。
void deleteAll()
{
SQLiteDatabase db= this.getWritableDatabase();
db.delete(table_name, null, null);
}
回答by dmkrush
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;
}
回答by Mr Chops
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()
)并且它起作用了。
回答by Suragch
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;
}
回答by Erick Moya
The right answer is this:
正确答案是这样的:
db.delete('tweets',"1",null);
From Android official documentation: SQLiteDatabase
来自Android官方文档:SQLiteDatabase
回答by Cristian
SQLite does not have any TRUNCATE
statement, so you must do:
SQLite 没有任何TRUNCATE
语句,因此您必须执行以下操作:
public void deleteall(){
SQLiteDatabase db = tweets.getWritableDatabase();
String delete = "DELETE FROM tweets";
db.rawQuery(delete, null);
}