Delete all data with SQLite delete syntax?

I made a button that deletes all the objects in my array that appear in my table. Then it reloads the data, and the tableview is empty. But how can I delete all data from my SQLite database, not just an array? Now the data appears upon restart. I tried this:

Void button:

- (void) deleteAllButton_Clicked:(id)sender { [appDelegate removeAllBooks:nil]; [self.tableView reloadData]; } 

Appdelegate void:

 -(void) removeAllBooks:(Book *)bookObj { //Delete it from the database. [bookObj deleteAllBooks]; //Remove it from the array. [bookArray removeAllObjects]; } 

Delete syntax in Book.m

 - (void) deleteAllBooks { if(deleteStmt == nil) { const char *sql = "WHAT DO I HAVE TO DO TO DELETE ALL THE ROWS?"; if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK) NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database)); } //When binding parameters, index starts from 1 and not zero. sqlite3_bind_int(deleteStmt, 1, bookID); if (SQLITE_DONE != sqlite3_step(deleteStmt)) NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database)); sqlite3_reset(deleteStmt); } 
+4
source share
3 answers

Well, the usual SQL syntax:

 DELETE FROM tablename 
+19
source

If you have a standard primary identifier column (and you should), you can do

 DELETE FROM tablename WHERE id > -1; 

And this will delete all rows in the table since there is no identifier less than 0.

+6
source

DELETE FROM tablename did not work for me. I used the sqlite3 database on the iPhone, and I assume there was a poster too. For me, for this to work, I need:

 DROP table tablename 

followed by the CREATE statement if I want the table to still exist (without any rows).

Of course, this requires knowing the correct CREATE statement. Using the SQLManager plugin for Firefox is quickly changing the design for me, the correct CREATE statement to use. I never understood why the DELETE FROM tablename does not work, but in my case it definitely wasn’t.

+2
source

Source: https://habr.com/ru/post/1336508/


All Articles