-1

如何检查用户名和密码是否与数据库中的用户名和密码相同。

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

视图控制器.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];
} 
}

即使我输入的用户名和密码与数据库中的完全相同,它仍然会向我显示错误消息,而不是显示下一个视图。

4

2 回答 2

2

你必须在objective-c中检查NSStrings

isEqualToString

对于您的示例:

if (![userText.text isEqualToString:logins.loginUser])

Check: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/isEqualToString:

于 2012-08-27T07:38:08.587 回答
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]; 
    }
}
于 2012-08-27T07:43:20.583 回答