我正在尝试在我的应用程序中注册用户。我有一个带有3 textfields (username, password and confirmpassword)submit按钮的视图控制器。

按下提交按钮时,将调用以下方法:

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

并且此方法由addUser方法调用:
-(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);
    }
}

addNewUser方法中,if (sqlite3_step(statement) == SQLITE_DONE)始终是false,语句之前有一些值
sqlite3_prepare_v2(_mDb, insert_stmt, -1, &statement, NULL);

但是在执行了以上语句后变成nil。我不明白为什么会这样。请帮忙。

最佳答案

这可能与您正在执行的检查有关,请尝试使用SQLITE_OK。它在docs中表示为在旧版接口(旧版接口)中,返回值将为 SQLITE_BUSY,SQLITE_DONE,SQLITE_ROW,SQLITE_ERROR或SQLITE_MISUSE 。使用“v2”接口,也可能会返回任何其他结果代码或扩展结果代码。

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

您还可以找到类似的问题及其答案here

在prepare语句后插入花括号,并在finalize语句后将其关闭。当您获得SQLITE_MISUSE时,可能是该例程被不适当地调用。可能是在已经完成的预准备语句或先前返回SQLITE_ERROR或SQLITE_DONE的语句上调用了它。也可能是两个或多个线程同时使用同一数据库连接。

希望这可以帮助 :)

关于ios - sqlite3_step(statement)== SQLITE_DONE始终为false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16891190/

10-13 02:55