【问题标题】:What is the query to select a table row , matching two column constraints in Sqlite选择表行的查询是什么,匹配Sqlite中的两个列约束
【发布时间】:2011-08-05 07:06:27
【问题描述】:

我正在尝试开发一个可以与数据库一起使用的登录程序。我使用了一个表emplopyee,列有“Userid”、“Passwd”和“Name”(这三个都是'VARCHAR'类型)。在我的程序中,我添加了两个文本字段“用户名”和“密码”。我得到了“用户名”和“密码”值并将它们存储在两个全局对象(类型为“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',原因:'获取用户名时出错'不是错误''

【问题讨论】:

    标签: iphone sqlite


    【解决方案1】:

    第一期:

    在 SQL WHERE 子句中,如果您有所有条件都必须为真的条件,则使用 AND 而不是逗号来组合它们:

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

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

    有关参考,请参阅SQL As Understood By SQLite

    第二期:

    您似乎错误地将函数 sqlite3_step() 的返回代码 SQLITE_DONE 视为“成功” - 消息“不是错误”是线索。也许value returned might be SQLITE_ROW - 表示查询成功,并且您有一些数据要读取?

    【讨论】:

    • 感谢您的回答。我会尝试更改它。我会尽快更新您的运行时错误。
    • 有教程here。查找名为 readAnimalsFromDatabase() 的方法,例如处理结果集。
    • 嘿确实看到了代码,现在我清除了这个异常.. 谢谢.. 但我的应用程序仍然无法工作.. 它在读取 sqlite3_open 语句时卡住了......
    【解决方案2】:

    在 WHERE 条件下,如果我们有多个要比较的参数,我们不能添加逗号(,)。选择查询应该像 NSString* type=@"开始"

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

    我们可以添加多个 AND,但不能添加逗号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 2022-11-28
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多