简体   繁体   English

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

[英]What is the query to select a table row , matching two column constraints in Sqlite

I was trying to develop a login program that would work with a database. 我正在尝试开发一个可以与数据库一起使用的登录程序。 I used a table emplopyee, with columns "Userid" , "Passwd" and "Name" (All the three are of type 'VARCHAR'). 我使用了一个表emplopyee,列“Userid”,“Passwd”和“Name”(这三个都是'VARCHAR'类型)。 In my program I have added two textfields "username" and "password". 在我的程序中,我添加了两个文本字段“username”和“password”。 I got the "username" and "password" values and stored them in two global objects(of type 'NSString'). 我得到了“用户名”和“密码”值,并将它们存储在两个全局对象中(类型为“NSString”)。
Now I would like to select the "Name" that matches with the given "Userid" and "Passwd". 现在我想选择与给定的“Userid”和“Passwd”匹配的“Name”。 The coding part which I used for the above reason is here: 我出于上述原因使用的编码部分在这里:

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

Its error free while compiling but the application does not work, it says some error in the query (on run time). 它在编译时没有错误,但是应用程序不起作用,它在查询中显示了一些错误(在运行时)。 if you can't figure out with the data given I'll provide you the whole code. 如果您无法弄清楚给出的数据,我将为您提供整个代码。

I have added the error details here: 我在这里添加了错误详细信息:
this error occurs if i use " , " (comma) in the select query: 如果我在选择查询中使用“,”(逗号),则会发生此错误:

*** Terminating app due to uncaught exception ***由于未捕获的异常而终止应用程序
'NSInternalInconsistencyException', reason: 'Error while reading Employee Name --> 'near ",": syntax error' ' 'NSInternalInconsistencyException',原因:'读取员工姓名时出错 - >'附近',“:语法错误''

and this one occurs when I use "AND" in the select query: 当我在select查询中使用“AND”时会发生这种情况:

*** Terminating app due to uncaught exception ***由于未捕获的异常而终止应用程序
'NSInternalInconsistencyException', reason: 'Error while getting the username 'not an error'' 'NSInternalInconsistencyException',原因:'获取用户名时出错'不是错误''

First issue: 首要问题:

In an SQL WHERE clause, if you have criteria that all need to be true you use AND, not a comma, to combine them: 在SQL WHERE子句中,如果您的条件都需要为true,则使用AND而不是逗号来组合它们:

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

If that's not it, suggest you edit your run-time error message details in to your question. 如果不是这样,建议您在问题中编辑运行时错误消息详细信息。

For reference, see SQL As Understood By SQLite . 供参考,请参阅SQLite理解的SQL

Second issue: 第二期:

It looks like you're incorrectly only treating return code SQLITE_DONE from function sqlite3_step() as 'success' - the message 'not an error' is the clue. 看起来你错误地只将函数sqlite3_step()中的返回码SQLITE_DONE视为'成功' - 消息'不是错误'是线索。 Perhaps the value returned might be SQLITE_ROW - indicating the query succeeded, and that you have some data to read? 也许返回值可能是SQLITE_ROW - 表示查询成功,并且您有一些数据要读取?

In WHERE condition we can't add comma(,) if we have multiple parameters to compare. 在WHERE条件中,如果我们有多个要比较的参数,我们就无法添加逗号(,)。 Select query should be like NSString* type=@"Start" 选择查询应该像NSString * type = @“Start”

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

We can add multiple AND's but not Commas. 我们可以添加多个AND而不是逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM