0

我注意到在我的应用程序中我正在做。当我注册一个用户名(例如“用户名”)并且当我尝试使用“用户名”登录时,它不会让我进入,但当我尝试“用户名”时它会。谁能告诉我如何解决它?这是我的代码。登记:

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2
@synthesize tbPassword,tbRepass,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    self.tbRepass.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnRegister:(id)sender
{
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s'",[tbUsername.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Username already exist!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        }
        else
        {
           if ([tbUsername.text isEqualToString:@""])
            {

                UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a username." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert2 show];
            }
            else if([tbPassword.text length]==0 || [tbRepass.text length]==0)
            {

                UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert3 show];
            }
            else if([tbPassword.text length]>0 && [tbRepass.text length]>0)
            {

                if([tbPassword.text isEqualToString:tbRepass.text])
                {
                    NSLog(@"Password Match");
                    NSString *username = tbUsername.text;
                    NSString *pass = tbPassword.text;

                    NSString *sql = [NSString stringWithFormat:@"INSERT INTO UserAccount ('Username','Password') VALUES ('%@','%@')",username,pass];
                    char *err;
                    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK)
                    {
                        sqlite3_close(users);
                        NSAssert(0, @"Could not update table)");

                    }
                    else
                    {
                        NSLog(@"table updated");
                        UIAlertView *alert4 = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was registered successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert4 show];
                        ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
                        VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

                       [self presentViewController:VC animated:YES completion:nil];

                        tbUsername.text = @"";
                        tbPassword.text = @"";


                    }

                }

                else
                {
                    UIAlertView *alert5 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Password does not match." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert5 show];
                }
            }
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
    [[self tbRepass]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

登录:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tbPassword,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnLogin:(id)sender {
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' and Password = '%s'",[tbUsername.text UTF8String],[tbPassword.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {
            ViewController3 *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
            VC.strUsername = self.tbUsername.text;
            [self presentViewController:VC animated:YES completion:nil];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Authentication Failed" message:@"Wrong Username/Password!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
}

- (IBAction)btnRegister:(id)sender {
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

brb 上课了。

4

2 回答 2

3

Change your query.

NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' COLLATE NOCASE and Password = '%s'",[tbUsername.text UTF8String],[tbPassword.text UTF8String]];


I think this will help.

于 2013-02-04T04:09:46.860 回答
1

在登录视图控制器上

第一的

将 username.text {用户使用 UI 添加的用户名} 更改为小写并将其保存在新的 NSString 中

第二

在 SELECT 语句中同时使用原始用户名和更改后的用户名 NSString

注意#您必须在 where 子句中使用“或”

简单的 :)

于 2013-02-04T07:05:33.330 回答