Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way in Android to delete all rows from a table

Tags:

android

sqlite

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.

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.

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

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

Hope that makes any sense

like image 996
Jester Avatar asked Oct 04 '13 14:10

Jester


People also ask

Which can be used to delete all the rows of a table in DBMS?

We can also use the TRUNCATE command to delete all the records from a table.

How do you delete all the row in the table without deleting the table?

Tip: You can delete the contents of a row or column without deleting the table structure. To do this, select the row or column and then press the Delete key.

How do you truncate a table in SQLite?

SQLite does not have an explicit TRUNCATE TABLE command like other databases. Instead, it has added a TRUNCATE optimizer to the DELETE statement. To truncate a table in SQLite, you just need to execute a DELETE statement without a WHERE clause. The TRUNCATE optimizer handles the rest.


2 Answers

Using DatabaseHelper you can do like this:

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

public void clearTable()   {
    database.delete(TABLE, null,null);
}
like image 130
XorOrNor Avatar answered Nov 15 '22 19:11

XorOrNor


Try

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

That will drop all the rows from <table_name>

like image 25
bclymer Avatar answered Nov 15 '22 18:11

bclymer