我正在尝试在我的应用程序中注册用户。
我有一个带有按钮的视图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
但在上述语句执行后转为。我不明白为什么会这样。
请帮忙。