Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
num is a integer and PassID also a integer
qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =num");
I need to select the row where the PassID matches
Iam getting error "unable to fetch row No query"

What I have tried:

qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =num");
Posted
Updated 21-Mar-18 22:21pm

1 solution

You have to create the query string passing num as parameter to a string formatting function. Because QSqlQuery::prepare() | Qt SQL 5.10[^] expects a QString argument, it can be be done in a single statement:
C++
qry.prepare(QString("SELECT * FROM ISSUEPASS WHERE PassID =%1").arg(num));

But SQL commands should never use created text strings to avoid SQL injections (here it would be OK because there is no text parameter). The preferred solution is using parametrised queries:
C++
qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =:passId");
qry.bindValue(":passId", num);
 
Share this answer
 
Comments
Member 13740197 26-Mar-18 4:31am    
It's working fine,but I had a problem when I am updating a parameter in the same row.

query.prepare("UPDATE ISSUEPASS SET INTIME =? WHERE PassID = :passId");
query.addBindValue(intime);
query.bindValue(":passId", num);
Here intime is QString variable,but Iam getting Sql error "Parameter Count mismatch"
Can you please tell me the solution.
Jochen Arndt 26-Mar-18 4:43am    
You are mixing the two value binding methods:
named binding and positional binding.

That is not supported. Use one method:

query.prepare("UPDATE ISSUEPASS SET INTIME =:intime WHERE PassID = :passId");
query.bindValue(":intime", intime);
query.bindValue(":passId", num);

OR

query.prepare("UPDATE ISSUEPASS SET INTIME = ? WHERE PassID = ?);
query.addBindValue(intime);
query.addBindValue(num);
Member 13740197 26-Mar-18 5:33am    
Thank you somuch for your answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900