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 回答