Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete All Data with SQLite delete syntax?

I've made a button that removes all the objects in my array, which is shown in my tableView. Then it reloads data and the tableview is empty. But how can I delete all the data from my SQLite database as well, and not just the array? Now the data occurs when I restart. I have tried this:

Button void:

   - (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);
}
like image 407
Leif Avatar asked Jan 21 '11 18:01

Leif


People also ask

How do I delete all in SQLite?

The TRUNCATE TABLE statement is used to remove all records from a table. 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.

How do I delete multiple records in SQLite?

format("DELETE FROM t WHERE ID='%s', "1' AND 1=1 --") // = "DELETE FROM t WHERE ID='1' AND 1=1 --'" => would delete all data!

Can we delete all rows using delete in SQL?

DELETE is a query that deletes one or multiple records from a table in SQL Server. With the statement, it is also possible to delete all data or specific data based on a condition specified in the WHERE clause. It is recommended to be careful and attentive while using DELETE , as it removes data permanently.


1 Answers

Well, the normal SQL syntax would be:

DELETE FROM tablename
like image 90
Lasse V. Karlsen Avatar answered Sep 20 '22 23:09

Lasse V. Karlsen