The code below partially works. Its supposed to return all the tables in the sqlite_master table and then delete them.
NSString *sql = [NSString stringWithFormat:@"SELECT name FROM sqlite_master WHERE type='table';"];
sqlite3_stmt *statement = [database prepare:sql];
if(statement != nil)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSString* currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
if ([currentTable rangeOfString:@"sqlite"].location == NSNotFound &&
[currentTable rangeOfString:@"Setting"].location == NSNotFound)
{
NSLog(@"current table: %@", currentTable);
[database deleteTable: currentTable];
}
}
sqlite3_finalize(statement);
}
However, after calling [database deleteTable: currentTable]; the tables are not being deleted.
delete table is just an sqlite3_exec which accepts the sql: [NSString stringWithFormat:@"DROP TABLE IF EXISTS '%@'", tableName]
Why is this? Is it because whilst i’m in the loop the database is in use? Would I be better putting the results in an array and than looping through and deleting tables that way? No exceptions are thrown/crashes etc and the exec is being executed.
Try finalizing using
sqlite3_finalizeafter every delete.Hope this helps you.