我正在尝试从数据库中删除创建行。但它没有这样做我有特定日期的内容在我的表中重复,所以我想删除特定日期的条目,并在每次建立连接时添加它们
我的代码是:
static sqlite3 *database = nil;
static sqlite3_stmt *deleteStmt = nil;
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"person.sqlite3"];
}
NSDate *today = [NSDate date];
NSLog(@"%@", today);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yy"];
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
!= SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
NSString * dateString = [dateFormat stringFromDate:today];
if(deleteStmt == nil)
{
const char *sql = "delete from SHOPPER where DATE = ?";
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_text(deleteStmt, 3, [dateString UTF8String],-1, NULL);
if (SQLITE_DONE != sqlite3_step(deleteStmt))
NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
sqlite3_reset(deleteStmt);
sqlite3_close(database);
答案 0 :(得分:0)
sqlite3_bind_text的第二个参数应为1,而不是3。 它意味着指数'?'准备好的陈述中的字符,和 因为只有一个'?'在您的删除语句中,它必须是1.
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_text(deleteStmt, 1, [dateString UTF8String], -1, NULL);
那么,为什么你将deleteStmt声明为static,并且只创建一次? 我认为它应该是为开放数据库创建的,因为你打开了 并且每次关闭数据库,你应该创建(并完成,而不是重置) 每次都是deleteStmt。