English 中文(简体)
在电话中,如何使Sqlite最后插入row?
原标题:in iphone how to get Sqlite last insert row id?

我在申请时想利用最新询问?

在我的申请中,有两阶段完成登记工作。

页: 1 (一) 登记表和提交纽特?

在点击提交纽特时,所有细节都应列入我的qlite表格。

页: 1

表格中节省的所有价值都应由用户在该网页上查看,如果他想要作任何改动,他应当能够这样做。 一旦用户编辑了某些价值,他就会继续播报所有数值,应当更新?

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

下面是我的代码第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];
        iii
        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];

                iii                   
            iii


        iii
    iii

从上述法典来看,我能够获得row,但我无法在更新询问时使用这种row。

以下是我的代码第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);
    iii

    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;    
         iii
iii

iii

问题回答
NSInteger lastRowId = sqlite3_last_insert_rowid(yourdatabasename);

sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);

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.

此外:

您重新准备发言(汇编了这 s),但您不再回避。 除此声明外,在再次使用该声明之前呼吁重新发表。 它节省了汇编。

EDIT:

sqlite3_prepare_v2 see a reference (&) to a statement. 避免这种情况与成员变数相,不要再打耳。 既然你重提结果,如果你再次使用这一发言,那么你需要将发言提一下qlite3_reset(reset)(),那么,他就会呼吁一步,并且将再次执行,而不必再补充(parsing) s。 这是优化的。

你没有检查你重新开车的返回代码。 航道检查和最低航标——但应当真正做到。 否则,你会默无声地在更难以发现的问题和错误之后转移。

EDIT:

每一点呼吁都可能失败或产生问题。 见以下脚注。 几乎每一项职能都使你回到返回法。 检查、记录和记录......

最后,你再次接近需要更加有力。 见这一SO员额的密切职能:

https://stackoverflow.com/questions/7762591/sqlite-in-ios-memory-issues/7762683#7762683>Sqlite in Observ issues

您需要制定<代码>rowid,而不是ID

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




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签