I am using sqlite3 for my iOS project.
When login/logout i want to recreate the whole db.
(i can do “delete from table;” but there are ~10 tables and when i need a table more i have to be careful about writing the delete code too)
So i searched for deleting the db; but i found that it can not be done with query.
It is said that i have to remove it from filesystem;
my code is;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *dbPathDB=[appDelegate getDBPath];//dbname.sqlite
//to remove the db
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"dbname.sqlite"];
//both of them works same if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if(filePath){
NSLog(@"it is removed now");
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
//recreating the db
if (sqlite3_open([dbPathDB UTF8String], &database) == SQLITE_OK) {
NSLog(@"db is created");
}
When i try it on device, when i first login (no removing database yet) everything is ok, when i logout and login again, i got errors about inserting the data (but it can connect to db, that part works, error is about inserting the same row again).
And i stop the device and run it again and login i got no insert errors because the db is clean..
It looks like db file is removed but db is on cache or sth like that? How can i remove and create the same db and then insert the data without errors?
This one works: