Clear all rows in table SQLite iOS

17,581

Solution 1

Normal SQL syntax to do this:

DELETE FROM tablename

Solution 2

any SQLite syntax query can be dealt using the SQLite Documentation

for the deletion of sqlite table rows in iOS the following code

NSString *query = @"delete from yourTable";
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
    // Read the data from the result row
    NSLog(@"result is here");
}

// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

i guess this solves your problem

Solution 3

If you want to delete all rows from SQL table then go for

DELETE FROM tablename

If you want to delete rows one by one the go for

DELETE FROM tablename WHERE id=2

Change id as per your requirement or you can mention the specific field name also whose row you want to delete

Solution 4

(void) DeleteRows {


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath =[documentsDir stringByAppendingPathComponent:@"gym.db"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
sqlite3_stmt *selectstmt;
if(!success)
{
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"gym.db"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success)
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

if (sqlite3_open([dbPath UTF8String], &contactDB) == SQLITE_OK) {
    //*************** insert value in database******************************\\

    NSString  *sql = [NSString stringWithFormat:@"delete from Offers"];
    const char *insert_stmt = [sql UTF8String];
    sqlite3_prepare_v2(contactDB,insert_stmt, -1, &selectstmt, NULL);
    if(sqlite3_step(selectstmt)==SQLITE_DONE)
    {
        NSLog(@"Delete successfully");
    }
    else
    {
        NSLog(@"Delete not successfully");

    }
    sqlite3_finalize(selectstmt);
    sqlite3_close(contactDB);
  }
}

Solution 5

- (IBAction)deleteAll:(id)sender { 

   NSString *tableName=@"Contacts";
   NSString *qsql = [NSString stringWithFormat:@"DELETE FROM %@",
                      tableName];
   sqlite3_stmt *statement;
    if (sqlite3_prepare_v2( db, [qsql UTF8String], -1,
                           &statement, NULL) == SQLITE_OK)
        while (sqlite3_step(statement) == SQLITE_DONE){
                        NSLog(@"%@", @"deleted");
        }
    else {
    sqlite3_close(db);
    NSAssert(0, @"Failed to Delete");
    }
   sqlite3_finalize(statement);

  }
Share:
17,581
LightNight
Author by

LightNight

Updated on June 14, 2022

Comments

  • LightNight
    LightNight almost 2 years

    i need your help.. How can I delete all rows in table from sqlite db? Please can you say how to delete all rows in one step (not one by one)..