i hope this is the correct platform to ask this.
i am doing a c++ application that incorporates qmysql, i am using qt. i have two qline edit fields being name and password. after the user has entered these two i want to use them for login. i have this query
query.prepare("SELECT Type FROM users WHERE Name = "+loginName+" AND Password = "+ loginPassword);
where could my error be. it compiles but get an error that says
"incorrect syntax near 'AND password =

It's not a C++ error; it's an SQL error. Get the C++ to spit out the complete query being sent so you know for sure what it's sending, and then find the error in it.

Edited 3 Years Ago by Moschops

I take it loginName and loginPassword are string objects.

string query = "SELECT Type FROM users WHERE Name = "+loginName+" AND Password = "+ loginPassword;
cout << query;

i extracted the qlineedit and stored the text in a qstring then i want to use that string ina query so no it doesnot come with qoutes. unless i donot undestand

I think that makes sense. So if loginName doesn't carry quotes, you'd end up with:

SELECT Type FROM users WHERE Name = someName AND Password = xxxx

...which is where I think your syntax error is coming from. String values need quotes:

SELECT Type FROM users WHERE Name = 'someName' AND Password = xxxx

You'll have to add them:

...WHERE Name = '"+loginName+"' AND...

Try that and see what happens.

Edited 3 Years Ago by gusano79

This article has been dead for over six months. Start a new discussion instead.