开发者

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

开发者 https://www.devze.com 2023-03-26 06:13 出处:网络
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\'). In my prog

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'). In my program I have added two textfields "username" and "password". I got the "username" and "password" values and stored them in two global objects(of type 'NSString').

Now I would like to select the "Name" that matches with the given "Userid" and "Passwd". 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 开发者_Python百科'%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' '

and this one occurs when I use "AND" in the select query:

*** Terminating app due to uncaught exception

'NSInternalInconsistencyException', reason: 'Error while getting the username 'not an error''


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:

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.

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. Perhaps the value returned might be SQLITE_ROW - indicating the query succeeded, and that you have some data to read?


In WHERE condition we can't add comma(,) if we have multiple parameters to compare. Select query should be like NSString* type=@"Start"

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

We can add multiple AND's but not Commas.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号