当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - I can't update data on sqlite3 database

问题描述:

I want to update user favorite data. Here is my code. Sqlite statement works correctly, but when I restart my app, favorite button is again 0.

This is my code. It works without error, but database does not change.

- (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];

}

How can I fix it?

网友答案:
  1. Did you fetch value from DB and update the content in the favorite button?
  2. Are you deleting the app and rerunning or just opening the closed app? The second way is the right way to test
  3. Log the path and go to the path open the db using some sqlite manager [ firefox extension has one] check value is entered into db
  4. Opening a closed app. Did you write the update data in button in viewDidLoad? Make it in viewWillAppear
网友答案:

while updating to Sqlite better fallow binding technique

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);
网友答案:

This is my solution. I added if (sqlite3_step(compiledStatement) == SQLITE_DONE) line and database is updated.

- (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);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: