如何循环SQLite行?

时间:2014-11-08 15:25:49

标签: ios sqlite

我想用SQLite DB设计登录和注册表单。我用三个字段和两个字段的登录表制作了注册表。

Registration form:
User Name:
Password:
RetypePassword:

Login form:
User Name:
Password:

如果有任何用户第一次尝试登录,我们应该向register.i.e提供一条消息,我们需要遍历注册表并检查登录字段是否已经存在。

所以我在这里错过了遍历注册表所有记录的逻辑(循环)。

- (void) loginBtn:(id)sender
{
     const char *dbpath = [_databasePath UTF8String];
     sqlite3_stmt    *statement;

     NSMutableArray *arr=[[NSMutableArray alloc]init];

     if (sqlite3_open(dbpath, &_registerDB) == SQLITE_OK)
     {
             NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM registertable;"];

             const char *query_stmt = [querySQL UTF8String];

             if (sqlite3_prepare_v2(_registerDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
             {
                     if (sqlite3_step(statement) == SQLITE_ROW)
                     {
                             NSString *regUserName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

                             NSString *regPassword = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];

                             NSString *str=[NSString stringWithFormat:@"%@%@",regUserName,regPassword];

                             [arr addObject:str];


                           //************************LOOP ?????????????

                           for(int i=o;i<arr.count;i++){

                                if([loginUserTxtFld.text isEqualToString:??????] && [loginPasswordTxtFld isEqualToString:??????]
                                 {
                                    NSLog(@"login successful");
                                 }

                                 else{
                                    NSLog(@"You have not yet registered");

                                     }

                           }


                             _statusLabel.text = @"Match found";
                     } else {
                             _status.text = @"Match not found";
                             _regUserNameTxtFld.text = @"";
                             _regPwdTxtFld.text = @"";
                     }
                     sqlite3_finalize(statement);
             }
             sqlite3_close(_registerDB);
     }
}

我需要更改哪些循环选定的行?

1 个答案:

答案 0 :(得分:0)

如果要循环,请替换

if (sqlite3_step(statement) == SQLITE_ROW) {
    // this only retrieves one row
}

while (sqlite3_step(statement) == SQLITE_ROW) {
    // this will be performed for each row
}

但是,更好的是,为什么不让SQLite为你做这项工作?

// NB: Do not use `stringWithFormat` to build this SQL, but rather use `?`
//     placeholders and then call `sqlite3_bind_text`

const char *query_stmt = "SELECT * FROM registertable WHERE username = ? AND password = ?";

if (sqlite3_prepare_v2(_registerDB, query_stmt, -1, &statement, NULL) != SQLITE_OK) {
    NSLog(@"prepare failed: %s", sqlite3_errmsg(_registerDB));
}

if (sqlite3_bind_text(statement, 1, [loginUserTxtFld.text UTF8String], -1, NULL) != SQLITE_OK) {
    NSLog(@"bind userid failed: %s", sqlite3_errmsg(_registerDB));
}

if (sqlite3_bind_text(statement, 2, [loginPasswordTxtFld.text UTF8String], -1, NULL) != SQLITE_OK) {
    NSLog(@"bind password failed: %s", sqlite3_errmsg(_registerDB));
}

int rc = sqlite3_step(statement);
if (rc == SQLITE_ROW) {
    // userid/password found
} else if (rc == SQLITE_DONE) {
    // userid/password not found, but no error
} else {
    NSLog(@"step failed: %s", sqlite3_errmsg(_registerDB));
}

// continue with finalize, close, or whatever else

显然,我不知道你的用户名和密码字段的名称,所以相应地改变这个SQL,但希望这说明了这个想法。