简体   繁体   English

在iphone中如何获取Sqlite最后插入行id?

[英]in iphone how to get Sqlite last insert row id?

I am making an application where i want to use update query? 我正在创建一个我想使用更新查询的应用程序?

In my application i have two stages to complete the registration process 在我的申请中,我有两个阶段来完成注册过程

in page1 i have registration form and one submit button? 在第1页我有注册表和一个提交按钮?

on click of submit button all the details should get insert into my sqlite table. 点击提交按钮,所有细节都应该插入到我的sqlite表中。

in page2 i have confirmation page and one edit and continue button? 在第2页我有确认页面和一个编辑和继续按钮?

all the value saved in a table should be view by the user on this page and if he want's to make any change he should be able to do that. 表中保存的所有值都应该由用户在此页面上查看,如果他想要进行任何更改,他应该能够做到这一点。 once user had edited some value and he press continue button all the value insert should get updated? 一旦用户编辑了一些值,他按下继续按钮,所有值插入都应该更新?

but i have try doing this but i am not able to update the last enter value? 但我尝试这样做,但我无法更新最后的输入值?

following is my code page1 insert code: 以下是我的代码page1插入代码:

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

                }                   
            }


        }
    }

from the above code i am able to get the row id but i am not able to use that rowid in update query 从上面的代码我能够获得行ID,但我无法在更新查询中使用该rowid

following is my code page2 Update code: 以下是我的代码页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;    
         }
}

} }

NSInteger lastRowId = sqlite3_last_insert_rowid(yourdatabasename);

sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); sqlite3_int64 sqlite3_last_insert_rowid(sqlite3 *);

http://www.sqlite.org/c3ref/last_insert_rowid.html http://www.sqlite.org/c3ref/last_insert_rowid.html

EDIT: call that function after you call the insert function, it will return you the id. 编辑:调用插入函数后调用该函数,它将返回id。

Also: 也:

You're preparing your statement (which compiles the sql) but you're not saving it off. 你正在准备你的语句(编译sql),但你没有保存它。 save a ref to that statement and call reset on it before using it again. 将ref保存到该语句并在再次使用之前调用reset。 It saves the compile. 它保存了编译。

EDIT: 编辑:

sqlite3_prepare_v2 returns a reference (&) to a statement. sqlite3_prepare_v2返回一个语句的引用(&)。 Save that off to a member variable and don't call prepare again. 将其保存到成员变量,不要再次调用prepare。 Since you're stepping through the results, if you re-use the statement, you need to call sqlite3_reset() on the statement, then call step and it will execute it again without re-compiling (parsing) the sql statement. 由于您正在逐步执行结果,如果您重新使用该语句,则需要在语句上调用sqlite3_reset(),然后调用step并再次执行它而无需重新编译(解析)sql语句。 This is an optimization. 这是一个优化。

You're not checking return codes from you're sqlite calls. 你没有检查你的sqlite调用的返回代码。 Always check and at a minimum log - but should really happen. 始终检查并至少记录日志 - 但应该真的发生。 Otherwise you're silently moving pass a problem and error later where it's harder to discover. 否则你会默默地移动传递问题,然后在更难发现的地方出错。

EDIT: 编辑:

each sqlite call can fail or have issues. 每个sqlite调用都可能失败或出现问题。 See the close function I reference below. 请参阅下面参考的关闭功能。 almost every function gives you back a return code. 几乎每个函数都会返回一个返回代码。 Check them, log them etc... 检查它们,记录它们等......

Finally, you're close needs to be more robust. 最后,你需要更加强大。 See the close function in this SO post: 请参阅此SO帖子中的关闭功能:

Sqlite in iOS memory issues iOS内存中的Sqlite问题

You need to set rowid instead of ID . 您需要设置rowid而不是ID the rest of code is fine 其余的代码很好

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM