我无法更新sqlite3数据库上的数据

时间:2014-04-17 20:30:05

标签: ios objective-c sqlite

我想更新用户喜欢的数据。这是我的代码。 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];
}

我该如何解决?

3 个答案:

答案 0 :(得分:0)

  1. 您是否从数据库中获取值并更新收藏夹按钮中的内容?
  2. 您是在删除应用并重新运行还是只是打开已关闭的应用?第二种方法是正确的测试方式
  3. 记录路径并转到路径打开数据库使用一些sqlite管理器[firefox扩展有一个]检查值输入db
  4. 打开已关闭的应用。您是否在viewDidLoad中的按钮中编写了更新数据?在viewWillAppear
  5. 中创建它

答案 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);
}