如何检查用户名和密码是否与数据库中的用户名和密码相同。
AppDelegate.m:
-(void) readLoginFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the Array
lo = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "Select * from Login";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aLoginName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aLoginPass = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aLoginAccess = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
// Create a new object with the data from the database
Login *los = [[Login alloc] initWithName:aLoginName pass:aLoginPass access:aLoginAccess];
// Add the object to the Array
[lo addObject:los];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
ViewController.m:
-(IBAction)buttonWasClicked:(id)sender
{
if ((userText.text != logins.loginUser) && (passText.text != logins.loginPass))
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:@"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
if (userText.text != logins.loginUser)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Username is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
if (passText.text != logins.loginPass)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Password is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
即使我输入的用户名和密码与数据库中的完全相同,它仍会显示错误消息,而不是显示下一个视图。
答案 0 :(得分:2)
您必须使用
检查objective-c中的NSStringsisEqualToString
对于你的例子:
if (![userText.text isEqualToString:logins.loginUser])
答案 1 :(得分:0)
您可以使用isEqualToString方法检查两个字符串是否相等....
if ([userText.text isEqualToString:logins.loginUser])
并应用这样的条件
-(IBAction)buttonWasClicked:(id)sender
{
Login *logins = nil;
logins= [lo objectAtIndexPath:0];
if ([userText.text isEqualToString:logins.loginUser] && [passText.text isEqualToString:logins.loginPass])
{
StudentViewController *studentView = [self.storyboard instantiateViewControllerWithIdentifier:@"studentView"];
[studentView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:studentView animated:YES completion:nil];
}
else if (![userText.text isEqualToString:logins.loginUser])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Username is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else if (![passText.text isEqualToString:logins.loginPass])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Password is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:[[NSString alloc] initWithFormat:@"Your Username/Password is incorrect!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}