如何编写查询以从iPhone应用程序中的sqlite数据库表中检索ID [英] How to write the query for retrieving id from sqlite database table in iphone apps

查看:52
本文介绍了如何编写查询以从iPhone应用程序中的sqlite数据库表中检索ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone应用程序中使用SQLite数据库是我的新手.我已经创建了一个带有登录表的数据库,并将该数据库添加到了项目中.登录表包含3个字段,分别是 ID Username Password 字段.我在视图的文本字段中输入用户名和密码.现在,我正在研究如何通过检查表输入正确的用户名和密码来从登录表中检索ID.有人可以帮忙吗?

I am new to using the SQLite database in iphone apps. I have created a database with login table in it and added the database to the project. Login table contains 3 fields ID, Username and Password fields. I am entering the username and password in the textfields of the view. Now I am looking of how to retrieve the id from the login table when username and password entered are correct by checking the table. Can anyone help out in this.

我的代码:

-(void)checkInDatabase
{
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        //uid = "select count(*) from logins where Username==txtusername.text and Password==txtpassword.text";
        NSString* sql =[[NSString alloc] initWithFormat:@"select id from login where Username =%s and Password=%s;",txtusername.text,txtpassword.text];

        [sql UTF8String];
        sqlite3_stmt *statement;
        if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_ROW);
            {
                uid =[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)]; 
            }
        }
        else
        {
            uid =0;
        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(database);
}

推荐答案

我通过编写如下代码使它正常工作:

I got it working by writing the code like this:

NSString* sql = [[NSString alloc] initWithFormat:
                                  @"select id from login where Username='%@' and  
                                  Password='%@'",txtusername.text,txtpassword.text];

现在对我很好.

这篇关于如何编写查询以从iPhone应用程序中的sqlite数据库表中检索ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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