如何编写查询以从iPhone应用程序中的sqlite数据库表中检索ID [英] How to write the query for retrieving id from sqlite database table in iphone apps
问题描述
在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屋!