我是ios Development的新手。你能帮我从表中删除一条记录吗?数据库和查询似乎很好。但我不知道为什么它不删除记录。我可以在控制台中看到nslog“profile deleted”。提前谢谢。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.str_databasePath = [documentsDirectory stringByAppendingPathComponent:@"MY_database.sqlite"];
sqlite3 *database = nil;
if (sqlite3_open([self.str_databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"database opened : %@", profileType);
const char* sqlStatement;
if([profilerType isEqualToString:@"personal"]){
sqlStatement = "DELETE FROM TABLE1 where profile_type = 'OWN_PERSONAL'";
}else if([profilerType isEqualToString:@"corporate"]){
sqlStatement = "DELETE FROM TABLE2 where profile_type = 'BANK_PROFILE'";
}
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"profile deleted ");
return YES;
}else{
NSAssert1(0,@"Error: Failed to prepare statement with message %s '.", sqlite3_errmsg(database));
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(database);
}
return NO;
答案 0 :(得分:3)
正如HeikoG所说,使用核心数据要容易得多。 Core Data基本上是一个SQLite数据库,但它更容易使用。看一下本教程:http://www.raywenderlich.com/934/core-data-tutorial-getting-started
如果您使用它而不是直接使用SQL,即使必须重新编写当前代码,也可以节省时间。
答案 1 :(得分:2)