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 =

Recommended Answers

All 6 Replies

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.

how would i print the query back

I take it loginName and loginPassword are string objects.

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

Does loginName come with quotes already?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.