Xcode登录屏幕sqlite3数据库身份验证问题 [英] Xcode login screen sqlite3 database authentication issue

查看:90
本文介绍了Xcode登录屏幕sqlite3数据库身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sqlite3为iphone应用程序创建登录屏幕,以存储登录凭据.

I am trying to create a login screen for an iphone application using sqlite3 to store the login credentials.

以下是我用来处理身份验证的功能:

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!";
        }
    }
}

这是我的数据库架构:

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

我认为我传递login.textpassword.text的详细信息的方式有问题.根据NSLog,它传递正确的值,但似乎无法检查它是否正确.似乎认为一切都很好,并且无论我输入什么内容都显示status.text = Congratulations.

I think there is something wrong with the way I am passing the details of login.text or password.text. According to NSLog it passes the right values but it seems to fail to check whether it is the right input or not. It seems to think everything is good and displays status.text = Congratulations no matter what I input.

我不知道如何解决此问题,将不胜感激.如果需要,我可以提供更多信息.预先感谢!

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!

推荐答案

从您发布的代码中,我想向您说明几件事,然后提出如何进行操作的建议.有4种主要的sqlite函数用于执行数据库操作

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

  1. sqlite3_open:打开数据库连接
  2. sqlite3_prepare_v2:将查询编译为字节码程序
  3. sqlite_bind:将输入值传递给查询
  4. sqlite3_step:评估/执行已编译的语句.

因此,根据您上面发布的代码,

So from the code you posted above,

  1. 您尚未执行步骤4.因此很明显,您根本没有执行查询.
  2. 您使用NSString stringWithFormat方法完全构成了查询(包括输入参数),因此无需使用sqlite_bind函数.
  3. 您用来验证用户身份的逻辑也有些混乱.

这是实现您所需要的方式

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");
      }
   }
 }

这篇关于Xcode登录屏幕sqlite3数据库身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆