简体   繁体   English

Xcode登录屏幕sqlite3数据库身份验证问题

[英]Xcode login screen sqlite3 database authentication issue

I am trying to create a login screen for an iphone application using sqlite3 to store the login credentials. 我正在尝试使用sqlite3为iPhone应用程序创建登录屏幕,以存储登录凭据。

The following is the function I am using to handle the authentication: 以下是我用来处理身份验证的功能:

-(void) enter
{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;

    if (sqlite3_open(dbpath, &DBLogin) == SQLITE_OK)
    {
        NSLog(@"Connected to Database");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT id FROM User WHERE id=\"%@\"",login.text];
        NSString *s = password.text;
        const char *query_stmt = [querySQL UTF8String];
        NSLog(@"String is %s",query_stmt);
        NSLog(@"Pass is %@",s);
        if (sqlite3_prepare_v2(DBLogin,query_stmt, -1, &statement, NULL) ==!SQLITE_NULL)
        {
            NSLog(@"Executed Correctly");
            querySQL = [NSString stringWithFormat:@"SELECT password FROM User WHERE password=\"%@\"",password.text];
            query_stmt = [querySQL UTF8String];
            const char *f= [login.text UTF8String];
            NSLog(@"String is %s",query_stmt);
            NSLog(@"Pass is %@",s);
            //   const char *l = [login.text UTF8String];
            //  sqlite3_bind_text(statement, 1, query_stmt, -1, SQLITE_STATIC);
            if (sqlite3_prepare_v2(DBLogin,query_stmt, -1, &statement, NULL) == !SQLITE_NULL)
            {
                if(sqlite3_bind_text(statement, 1,f , -1, SQLITE_STATIC))
                    status.text= @"Congratulations";
            }
            else
            {
                NSLog(@"error");
            }
        }
        else
        {
            NSLog(@"Failed");
            status.text =@"ERROR!";
        }
    }
}

And this is my schema for the database: 这是我的数据库架构:

CREATE TABLE User (id varchar(20) PRIMARY KEY, password varchar(20));

I think there is something wrong with the way I am passing the details of login.text or password.text . 我认为我传递login.textpassword.text的详细信息的方式有问题。 According to NSLog it passes the right values but it seems to fail to check whether it is the right input or not. 根据NSLog它传递正确的值,但似乎无法检查它是否正确。 It seems to think everything is good and displays status.text = Congratulations no matter what I input. 似乎认为一切都很好,并显示status.text = Congratulations无论我输入了什么。

I don't have any idea how to fix this and would appreciate any help I can get. 我不知道如何解决此问题,将不胜感激。 I can provide more information if needed. 如果需要,我可以提供更多信息。 Thanks in advance! 提前致谢!

From the code you posted, I want to make few things clear for you and then will suggest how to proceed. 从您发布的代码中,我想向您说明几件事,然后建议如何进行。 There are 4 main sqlite functions used to perform a database operation 有4个主要的sqlite函数用于执行数据库操作

  1. sqlite3_open : To open a database connection sqlite3_open:打开数据库连接
  2. sqlite3_prepare_v2: To compile the query into byte-code program sqlite3_prepare_v2:将查询编译为字节码程序
  3. sqlite_bind : To pass input values to the query sqlite_bind:将输入值传递给查询
  4. sqlite3_step : To evaluate/execute the compiled statement. sqlite3_step:评估/执行已编译的语句。

So from the code you posted above, 因此,根据您上面发布的代码,

  1. you have not performed step 4. So its obvious that you have not executed the query at all. 您尚未执行步骤4。因此很明显,您根本没有执行查询。
  2. you formed the query completely(including the input parameter) using NSString stringWithFormat method, so there is no need to use sqlite_bind function. 您使用NSString stringWithFormat方法完全构成了查询(包括输入参数),因此无需使用sqlite_bind函数。
  3. The logic you used to authenticate user is also a bit confusing. 您用来验证用户身份的逻辑也有些混乱。

Here is the way to achieve what you need 这是实现您所需要的方式

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
   const char *sql = "select * from User where id = ? and password = ?";
   sqlite3_stmt *selectstmt;
   if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
     sqlite3_bind_text(selectstmt, 1, [login.text UTF8String], -1, SQLITE_TRANSIENT);
     sqlite3_bind_text(selectstmt, 2, [password.text UTF8String], -1, SQLITE_TRANSIENT);
      while(sqlite3_step(selectstmt) == SQLITE_ROW) {

          NSLog(@"Successful login");
      }
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM