数据库没有更新

时间:2012-03-08 05:48:48

标签: iphone ios database sqlite

我正在使用数据库。当我第一次更新我的字段数据库正在更新。但是当我尝试更新下次没有更新但它显示数据库正在更新。我知道问题出在哪里.Thanks!

-(void) updateData {

    sqlite3_stmt    *statement;

    details = [updateArray objectAtIndex:0];

    NSString *destinationPath = [self getDestinationPath];

    const char *dbpath = [destinationPath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        NSString *updateSQL = [NSString stringWithFormat: @"UPDATE BirthdayRemainderList SET FirstName=\"%@\", LastName=\"%@\",Dob=\"%@\",Address=\"%@\",City=\"%@\",State=\"%@\",Email=\"%@\",Phone=\"%@\" WHERE ids=%@",details.firstNameString,details.lastNameString,details.dobString,details.addressString,details.cityString,details.stateString,details.emailString,details.PhoneString,details.ids];

        const char *insert_stmt = [updateSQL UTF8String];

        sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Row updated");

        } 

        else {
            NSLog(@"Failed to update row");
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }

}

我的更新代码在这里

-(IBAction) update
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    savedData = [[DetailsClass alloc] init];

    savedData.firstNameString = firstName.text;
    savedData.lastNameString = lastName.text;
    NSLog(@"last name is %@",savedData.lastNameString);
    savedData.dobString = dob.text;
    savedData.addressString = address.text;
    savedData.cityString = city.text ;
    NSLog(@"Changed city value is %@",savedData.cityString);
    savedData.stateString = state.text;
    savedData.emailString = email.text;
    NSLog(@"Changes state value is %@",savedData.emailString);
    savedData.PhoneString =  phone.text;
    NSLog(@"Phone no is %@",savedData.PhoneString);
    savedData.ids = idString;
    NSLog(@"details in id %@",savedData.ids);
    [delegate.updateArray addObject:savedData];
    [delegate updateData];

}

这会犯任何错误吗? 如果我们使用两个不同的数组进行保存和更新,它会有什么不同吗?

让我知道问题出在哪里。谢谢!

1 个答案:

答案 0 :(得分:1)

添加sqlite错误消息以更深入地了解真正的问题。

sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
        NSLog(@"Row updated");

    } 

    else {
        NSLog(@"Failed to update row %s", sqlite3_errmsg(database));
    }

如果sql db很忙,它会发生。如果你在firefox的插件等其他地方打开过,请确保关闭数据库。