如何在iphone中更新Sqlite最后插入的行ID?

时间:2011-11-04 07:18:16

标签: iphone objective-c ios iphone-sdk-3.0 xcode3.2

我正在创建一个我想使用更新查询的应用程序?

在我的申请中,我有两个阶段来完成注册过程

第1页中的

我有注册表和一个提交按钮吗?

点击提交按钮,所有细节都应该插入到我的sqlite表中。

第2页中的

我有确认页面和一个编辑并继续按钮?

表中保存的所有值都应该由用户在此页面上查看,如果他想要进行任何更改,他应该能够做到这一点。一旦用户编辑了一些值,他按下继续按钮,所有值插入都应该更新?

但我尝试这样做,但我无法更新最后的输入值?

以下是我的代码页1插入代码:

-(void)submit

{

if( ([UserName.text isEqualToString:@""]) || ([Password.text isEqualToString:@""]) ||

([ConfirmPassword.text isEqualToString:@""]) || ([Name.text isEqualToString:@""]) ||

([Email.text isEqualToString:@""]) || ([ContactNO.text isEqualToString:@""]) ||

([MobileNo.text isEqualToString:@""]) || ([Address.text isEqualToString:@""]) )

{

    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!" 
                                                             message:@"Please fill in the details." delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil, nil];
        [ErrorAlert show];
        [ErrorAlert release];
    }
    else 
    {
        Confirmation_form *conForm = [[Confirmation_form alloc] initWithNibName:@"Confirmation_form" bundle:nil];
        conForm.data = UserName.text;
        conForm.data1 = Password.text;
        conForm.data2 = ConfirmPassword.text;
        conForm.data3 = Name.text;
        conForm.data4 = Email.text;
        conForm.data5 = ContactNO.text;
        conForm.data6 = MobileNo.text;
        conForm.data7 = Address.text;


        sqlite3_stmt  *statement;
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
        {



            NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO test(UserName, Password, ConfirmPassword, Name, Email, ContactNO, MobileNo, Address) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",UserName.text, Password.text, ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text];
            const char *insert_stmt = [insertSQL UTF8String];
            sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
            if(sqlite3_step(statement) == SQLITE_DONE)
            {
                //status.text = @"Contact added";
                UserName.text = @"";
                Password.text = @"";
                ConfirmPassword.text = @"";
                Name.text = @"";
                Email.text = @"";
                ContactNO.text = @"";
                MobileNo.text = @"";
                Address.text = @"";


                sqlite3_last_insert_rowid;
                rowID = sqlite3_last_insert_rowid(test1DB);
                NSLog(@"last inserted rowId = %d",rowID);

                sqlite3_reset(statement);
                sqlite3_finalize(statement);
                sqlite3_close(test1DB);


                [self.navigationController pushViewController:conForm animated:YES];

            }                   
        }


    }
}

从上面的代码我能够获得行ID但我无法在更新查询中使用该rowid

以下是我的代码页2更新代码:

-(IBAction)Update;

{

sqlite3_stmt  *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)

{


NSString *insertSQL = [NSString stringWithFormat:@"UPDATE test SET         UserName='%@',Password='%@',ConfirmPassword='%@',Name='%@',Email='%@',ContactNO='%@',MobileNO='%@',Address='%@'WHERE ID='%@'",UserName.text,Password.text,ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text, sqlite3_last_insert_rowid(test1DB)];

const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
    sqlite3_step(statement);
                            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                [alert show];
                                [alert release];
                                alert = nil;    
    sqlite3_finalize(statement);
    sqlite3_close(test1DB);
}

else {
    sqlite3_step(statement);
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record notadded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    [alert release];
                    alert = nil;    
     }
}

1 个答案:

答案 0 :(得分:0)

简单方法 - >创建“someconstant.h”文件在那里声明你的_rowID变量。

在您的班级中导入文件,在第一堂课中分配您的价值,并在第二堂课中使用它。