Android中删除表中所有行的最佳方法是什么

时间:2022-09-16 14:31:12

I have a WCF witch provides some JSON data and then I save it into the local database. Works fine I have a Activity witch fills the data from a local database to a listview witch works fine.

我有一个WCF女巫提供一些JSON数据,然后我将其保存到本地数据库。工作正常我有一个活动女巫将数据从本地数据库填充到listview女巫工作正常。

public void fillData() {

    // Fields from the database (projection)
    // Must include the _id column for the adapter to work
    String[] from = new String[] { TodoTable.COLUMN_SUMMARY };
    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label };

    getLoaderManager().initLoader(0, null, this);
    adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from,
            to, 0);

    lw.setAdapter(adapter);
}

What I can't figure out is what would be the best way to delete all rows prior to a sync action from a WCF.

我无法弄清楚是什么是从WCF同步操作之前删除所有行的最佳方法。

I could do this by getting all Id's from the database then find the row URI and then use:

我可以通过从数据库中获取所有Id然后找到行URI然后使用:

getContentResolver().delete(uri, null, null)

I just think that there has to be a better way I saw many examples on the net that uses the DbHelper class but I can't figure out how to access the dbHelper class from the Activity or over the ContentProvider

我只是认为必须有更好的方法我在网上看到许多使用DbHelper类的例子,但我无法弄清楚如何从Activity或ContentProvider访问dbHelper类

Hope that makes any sense

希望这是有道理的

4 个解决方案

#1


29  

Using DatabaseHelper you can do like this:

使用DatabaseHelper你可以这样做:

 dbHelper = new DatabaseHelper(context);
 database = dbHelper.getWritableDatabase();

public void clearTable()   {
    database.delete(TABLE, null,null);
}

#2


6  

Try

尝试

<your-SQLite-instance>.execSQL("DELETE FROM <table_name>");

That will drop all the rows from <table_name>

这将从 中删除所有行

#3


2  

Try using this code to delete all rows in a table :

尝试使用此代码删除表中的所有行:

database.delete(tablename, null,null);

Just replace database for your db name and tablename for the table you want to delete the rows

只需替换要删除行的表的数据库名称和表名的数据库

#4


-1  

If you just want to delete all the rows on a table:

如果您只想删除表中的所有行:

drop table if exists "tablename"

And then recreate the table with the appropriate columns. I personally find it a lot easier to do it this way.

然后使用适当的列重新创建表。我个人认为这样做更容易。

#1


29  

Using DatabaseHelper you can do like this:

使用DatabaseHelper你可以这样做:

 dbHelper = new DatabaseHelper(context);
 database = dbHelper.getWritableDatabase();

public void clearTable()   {
    database.delete(TABLE, null,null);
}

#2


6  

Try

尝试

<your-SQLite-instance>.execSQL("DELETE FROM <table_name>");

That will drop all the rows from <table_name>

这将从 中删除所有行

#3


2  

Try using this code to delete all rows in a table :

尝试使用此代码删除表中的所有行:

database.delete(tablename, null,null);

Just replace database for your db name and tablename for the table you want to delete the rows

只需替换要删除行的表的数据库名称和表名的数据库

#4


-1  

If you just want to delete all the rows on a table:

如果您只想删除表中的所有行:

drop table if exists "tablename"

And then recreate the table with the appropriate columns. I personally find it a lot easier to do it this way.

然后使用适当的列重新创建表。我个人认为这样做更容易。