我正在尝试删除数据库中的所有行,因为我编写了以下代码..
- (id)init {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"dataBase.sqlite3"];
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
if (!databaseAlreadyExists){
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"dataBase" ofType:@"sqlite3"];
[[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
if (sqlite3_open([databasePath UTF8String], &_database) == SQLITE_OK){
}
return self;
}
-(void)deleteAll{
NSString *query = @"DELETE FROM user_info";
sqlite3_stmt *statement = nil;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
NSLog(@"Should delete..");
}
sqlite3_finalize(statement);
sqlite3_close(_database);
}
但是当我重建app时,数据会出现。我做错了什么?
答案 0 :(得分:0)
尝试条件if:
if(sqlite3_prepare_v2(database, sql, -1, &deletestmt, NULL) != SQLITE_OK)
答案 1 :(得分:0)
您永远不会致电sqlite3_step
来实际执行查询。
此外,您在错误的位置关闭数据库。你的结束应该在开放的对面完成。
答案 2 :(得分:0)
首先,在打开数据库之后获取数据库路径
NSString *dbPath1=[SqliteManager getDataBasePath];
if (sqlite3_open([dbPath1 UTF8String], &database) == SQLITE_OK)
{
NSLog(@"opened the data base");
const char *sql = "delete from travels";
if(sqlite3_prepare_v2(database, sql, -1, &deletestmt, NULL) == SQLITE_OK)
if(SQLITE_DONE != sqlite3_step(deletestmt))
NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
else
isdeleted=YES;
}
sqlite3_reset(deletestmt);
sqlite3_close(database);
write in ur method inside.....
Getting Database Path....
+(NSString *)getDataBasePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSLog(@"file Path is %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"Give ur Sqlite Filename"];
}