如何在iphone中使用sqlite创建登录表单?

时间:2015-08-06 09:49:15

标签: ios objective-c iphone xcode sqlite

我已经为登录视图创建了注册页面我需要执行查询才能成功登录这里是我的代码

Register.h

@interface register1 : UIViewController{
    sqlite3 *logindb;

    NSString *databasePath;

}
-(IBAction)submit:(id)sender;
@property(retain, nonatomic)IBOutlet UITextField *username;
@property(retain, nonatomic)IBOutlet UITextField *password;
@property(retain, nonatomic)IBOutlet UILabel *status;

register.m

@synthesize username,password,status;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"login.db"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &logindb) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS LOGIN(ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, PASSWORD TEXT)";


            if (sqlite3_exec(logindb, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"Failed to create table") ;            }

            sqlite3_close(logindb);

        } else {
            NSLog( @"Failed to open/create database");
        }
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)submit:(id)sender{
    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &logindb) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOGIN (username, password) VALUES (\"%@\",\"%@\" )", username.text, password.text];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(logindb, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            status.text = @"Registartion Done";
            username.text = @"";
            password.text = @"";

        } else {
            status.text = @"Failed to Register";
        }
        sqlite3_finalize(statement);
        sqlite3_close(logindb);
    }


}

login.h

    IBOutlet UITextField *username;
    IBOutlet UITextField *password;

    -(IBAction)submit:(id)sender;

login.m

    -(IBAction)submit:(id)sender{
        if([username.text isEqualToString:@""] || [password.text isEqualToString:@""]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Here comes a block." message:@"You missed something, please check your details and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
    }
        else{
            NSString *query=[NSString stringWithFormat:@"select * from login where username=\"%@\" and password=\"%@\"",username.text,password.text];
            NSLog(@"%@",query);
             }

我能够成功注册但无法成功登录以执行查询

0 个答案:

没有答案