Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

ios - sqlite3_step(statement) == SQLITE_DONE is always false

I am trying to Sign up a user in my application.
I have a view controller with 3 textfields (username, password and confirmpassword) and a submit button.

The following method is called when submit button is pressed:

-(IBAction)addUser
{
    NSString *tempUser,*tempPass, *tempConfPass;
    tempUser = [[NSString alloc]init];
    tempPass = [[NSString alloc]init];
    tempConfPass = [[NSString alloc]init];
    tempUser = [NSString stringWithFormat:@"%@",_mUserName.text];
    tempPass = [NSString stringWithFormat:@"%@",_mPassword.text];
    tempConfPass = [NSString stringWithFormat:@"%@",_mConfPassword.text];
    signupUser = [[UseDb alloc]init];

    flagUser = [signupUser addNewUser:_mUserName.text:_mPassword.text:_mConfPassword.text];
    if(flagUser)
    {
        myAlertViewUser = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User Added"
                                                    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

        [myAlertViewUser show];
    }
    else {
       _mStatus.text = @"failed to add user";
       myAlertViewUser = [[UIAlertView alloc] initWithTitle:@"Error" message:@"passwords don't match"
                                delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

       [myAlertViewUser show];
    }
}

and this method is called by addUser method:

-(BOOL)addNewUser:(NSString *)newUser :(NSString *)newPassword :(NSString *)confirmPass
{
   NSLog(@"%@....%@...%@",newUser, newPassword, confirmPass);
    sqlite3_stmt    *statement;
    const char *dbpath = [_mDatabasePathDb UTF8String];

    if (sqlite3_open(dbpath, &_mDb) == SQLITE_OK && [newPassword isEqualToString:confirmPass] && ![newUser isEqualToString:@""] && ![newPassword isEqualToString:@""])
    {
        self.userName = [NSString stringWithFormat:@"%@",newUser];
        self.password = [NSString stringWithFormat:@"%@",newPassword];

        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO USERDETAIL VALUES ("%@","%@","%@", "%@","%@", "%@","%@", "%@","%@", "%@","%@")",self.userName,self.password,@"",@"",@"",@"",@"",@"",@"",@"",@"" ];

        NSLog(@"%@",insertSQL);
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            return YES;
            /*    mUserName.text = @"";
             mPassword.text = @"";
             mConfPassword.text = @""; */

        }
        else {

            NSLog(@"failed to add user");
        }

        sqlite3_finalize(statement);
        sqlite3_close(_mDb);
    }
}

In the addNewUser method, if (sqlite3_step(statement) == SQLITE_DONE) is always coming out to be false, statement has some value before

sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, NULL); 

but turns to nil after the above statement is executed. I don't understand why that is happening.
Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This can be an issue with the check you are doing, try SQLITE_OK. Its said here in docs as In the legacy interface(older interface), the return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. With the "v2" interface, any of the other result codes or extended result codes might be returned as well.

if (sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, nil) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
               return YES;
            }
            else 
            {
              NSLog(@"failed to add user");
            }
            sqlite3_finalize(statement);
        }

you can also find a similar question and its answer here

Insert a curly brace after the prepare statement and close it after finalize statement. As you get SQLITE_MISUSE, it can be that this routine was called inappropriately. Perhaps it was called on a prepared statement that has already been finalized or on one that had previously returned SQLITE_ERROR or SQLITE_DONE. Or it could be the case that the same database connection is being used by two or more threads at the same moment in time.

Hope this helps :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...