我想更新用户喜欢的数据。这是我的代码。 Sqlite语句正常工作,但当我重新启动我的应用程序时,收藏夹按钮再次为0.
这是我的代码。它可以正常工作,但数据库不会改变。
- (void)favoriteButton: (UIButton *)sender
{
NSString *fav = self.sights[sender.tag][@"S_Favorite"];
if ([fav isEqualToString:@"0"]) {
fav = @"1";
}
else {
fav = @"0";
}
databaseName = @"tripvel.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Setup the database object
sqlite3 *database;
NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITY_SIGHTS SET S_Favorite = '%@' WHERE rowid = '%@'", fav, self.sights[sender.tag][@"rowid"]];
NSLog(@"%@", querySQL);
const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
if (SQLITE_DONE != sqlite3_step(compiledStatement))
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
else{
sqlite3_reset(compiledStatement);
self.sights[sender.tag][@"S_Favorite"] = fav;
NSLog(@"1");
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
- (void) checkAndCreateDatabase
{
databaseName = @"tripvel.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success)
return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
我该如何解决?
答案 0 :(得分:0)
答案 1 :(得分:0)
更新为Sqlite时更好的休闲绑定技术
NSArray * pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * filePath = [pathArray objectAtIndex:0];
filePath =[filePath stringByAppendingPathComponent:@"tripvel.sqlite"];
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt *statement;
NSString *insertSQL = [NSString stringWithFormat:@"update CITY_SIGHTS set S_Favorite=? where rowid=?"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [S_FavoriteName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 2, rowid);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Updated");
}else
NSLog(@"Not updated");
sqlite3_finalize(statement);
}
sqlite3_close(database);
答案 2 :(得分:0)
这是我的解决方案。我添加了if(sqlite3_step(compiledStatement)== SQLITE_DONE)行和数据库更新。
- (void)favoriteButton: (UIButton *)sender
{
NSString *fav = self.sights[sender.tag][@"S_Favorite"];
if ([fav isEqualToString:@"0"]) {
fav = @"1";
}
else {
fav = @"0";
}
databaseName = @"tripvel.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Setup the database object
sqlite3 *database;
NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITY_SIGHTS SET S_Favorite = '%@' WHERE rowid = '%@'", fav, self.sights[sender.tag][@"rowid"]];
const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
if (sqlite3_step(compiledStatement) == SQLITE_DONE)
{
NSLog(@"Updated");
}else
NSLog(@"Not updated");
}
else{
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}