如何检查用户名和密码是否与数据库中的用户名和密码相同。
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];
}
}
即使我输入的用户名和密码与数据库中的完全相同,它仍然会向我显示错误消息,而不是显示下一个视图。