选择表行的查询是什么,匹配Sqlite中的两个列约束

时间:2011-08-05 07:06:28

标签: iphone sqlite

我正在尝试开发一个可以与数据库一起使用的登录程序。我使用了一个表emplopyee,列“Userid”,“Passwd”和“Name”(这三个都是'VARCHAR'类型)。在我的程序中,我添加了两个文本字段“username”和“password”。我得到了“用户名”和“密码”值,并将它们存储在两个全局对象中(类型为“NSString”) 现在我想选择与给定的“Userid”和“Passwd”匹配的“Name”。 我出于上述原因使用的编码部分在这里:

sqlite3 *dB;
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &dB)==SQLITE_OK){
    const char *sql="Select Name from employee Where Userid=? , Passwd=?";

           if(sqlite3_prepare_v2(dB, sql, -1, &compiledStatement, NULL)!=SQLITE_OK)
    NSAssert1(0,@"Error while reading Employee Name -->%s',sqlite3_errmsg(dB));
}
sqlite3_bind_text(compiledStatement,1,[txt UTF8String],-1,SQLITE_TRANSIENT);
   /*txt and txt1 are the global objects i told about they have the values of Userid  and Passwd from the texxt fields respectively*/
sqlite3_bind_text(compiledStatement,2,[txt1 UTF8String],-1,SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(compiledStatement))
 txt3 =[[NSString alloc] initWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)];


else
    NSAssert1(0, @"Error while getting the username '%s'", sqlite3_errmsg(dB));         
    sqlite3_reset(compiledStatement);

编译时它没有错误,但是应用程序不起作用,它在查询中显示了一些错误(在运行时)。 如果您无法弄清楚给出的数据,我将为您提供整个代码。

我在这里添加了错误详情:
 如果我在选择查询中使用“,”(逗号),则会出现此错误:

  

***因未捕获的例外而终止应用程序
   'NSInternalInconsistencyException',原因:'读取员工姓名时出错 - > 'near',“:语法错误''

当我在选择查询中使用“AND”时会发生这种情况:

  

***因未捕获的例外而终止应用程序
  'NSInternalInconsistencyException',原因:'获取用户名时出错'不是错误''

2 个答案:

答案 0 :(得分:6)

第一期:

在SQL WHERE子句中,如果您的条件都需要为true,则使用AND而不是逗号来组合它们:

SELECT Name FROM employee WHERE Userid=? AND Passwd=?

如果不是这样,建议您在问题中编辑运行时错误消息详细信息。

供参考,请参阅SQL As Understood By SQLite

第二期:

看起来你只是错误地将函数sqlite3_step()中的返回码SQLITE_DONE视为'success' - 消息'not an error'是线索。也许value returned might be SQLITE_ROW - 表示查询成功,并且您有一些数据需要阅读?

答案 1 :(得分:0)

在WHERE条件中,如果我们有多个要比较的参数,我们就无法添加逗号(,)。选择查询应该是这样的 NSString * type = @“开始”

@“SELECT * FROM images WHERE image_status = 0 AND image_id = 112 AND image_type = type”

我们可以添加多个AND而不是逗号。