0

SQLite3を使用しています。次のコードを使用して、既に存在する行を更新しています

- (BOOL)updateTableData :(NSString *) num
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    dbPath = [self getDBPath:@"studentslist.sqlite"];
    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
    sqlite3_stmt *updateStmt;
    const char *sql = "update studentInfo set sAge=?, sAddrs1=?, sAddrs2=?, sMobile =?, sClass =? where sRollNumber=?;";
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
    {
        NSLog(@"updateTableData: Error while creating delete statement. '%s'", sqlite3_errmsg(database));
        return;
    }
    NSLog(@"appDelegate.updateArray %@",appDelegate.updateArray);
    sqlite3_bind_int(updateStmt, 1,[[appDelegate.updateArray objectAtIndex:0] intValue]);
    sqlite3_bind_text(updateStmt, 2,[[appDelegate.updateArray objectAtIndex:1] UTF8String], -1,SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 3,[[appDelegate.updateArray objectAtIndex:2] UTF8String], -1,SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 4,[[appDelegate.updateArray objectAtIndex:3] UTF8String], -1,SQLITE_TRANSIENT);
    sqlite3_bind_text(updateStmt, 5,[[appDelegate.updateArray objectAtIndex:4] UTF8String], -1,SQLITE_TRANSIENT);

    if (SQLITE_DONE != sqlite3_step(updateStmt)) 
    {
        NSLog(@"updateTableData: Error while updating. '%s'", sqlite3_errmsg(database));
        sqlite3_finalize(updateStmt);
        sqlite3_reset(updateStmt);
        sqlite3_close(database);
        return NO;
    }
    else
    {
        NSLog(@"updateTableData: Updated");
        sqlite3_finalize(updateStmt);
        sqlite3_close(database);
        return  YES;
    }
}
else
{
    NSLog(@"updateTableData :Database Not Opened");
    sqlite3_close(database);
    return NO;
}

メインクラスでは、このメソッドを次のように呼び出しています

-(IBAction)updateButtonAction:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
db = [[Database alloc]init];
if ([ageTxtFld.text length]>0 && [address1TxtFld.text length]>0 && [address2TxtFld.text length]>0 && [mobileTxtFld.text length]>0 && [classTxtFld.text length]>0) 
{
    [appDelegate.updateArray addObject:ageTxtFld.text];
    [appDelegate.updateArray addObject:address1TxtFld.text];
    [appDelegate.updateArray addObject:address2TxtFld.text];
    [appDelegate.updateArray addObject:mobileTxtFld.text];
    [appDelegate.updateArray addObject:classTxtFld.text];
    NSLog(@"appDelegate.updateArray %@",appDelegate.updateArray);
    NSString *num = rollNumberTxtFld.text;
    BOOL is = [db updateTableData:num];
    if (is == YES)
    {
        updateAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Updated" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [updateAlert show];
    }
    else
    {
        updateAlert = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Not Updated" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [updateAlert show];
    }
}
else
{
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter all values" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [errorAlert show];
}
}

ただし、常にNSLog(@"updateTableData: Updated"); を出力します。しかし、更新していません

誰でも私を提案したり、コードを手伝ったりできます。

前もって感謝します

4

1 に答える 1

0
- (void)createEditableCopyOfDatabaseIfNeeded {

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"studentslist.sqlite"];
    dbPath =writableDBPath;
    [databasePath retain];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"studentslist.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    writableDBPath = nil;
    documentsDirectory = nil;
    paths = nil;
}

このメソッドをコードに記述して呼び出します。確かにこれはあなたのコードの問題でした。

于 2012-07-06T09:57:36.533 に答える