PDA

View Full Version : Multiple sql queries



fantom
28th February 2011, 14:21
Hello all,

I would like your help concerning multiple sql queries in qt.

At the moment i am trying to insert in a table a value and the same time to check if this value is already exists in an other table. Could you please give me an example how this can be done?

Please see below how i tried to do it. I always get "Invalid device ID".



QSqlQuery query;
QString strQuery;

strQuery="INSERT INTO User VALUES ( :name, :psw, :dID)";

query.bindValue( ":name", ui.RUserNameLineEdit->text() );
query.bindValue( ":psw",ui.RPasswordLineEdit->text() );

if (ui.RUserNameLineEdit->text() == 0)
{
printf("Invalid UserName\n");
return;
}
if (ui.RPasswordLineEdit->text() == 0)
{
printf("Invalid password\n");
return;
}
if (ui.ConfirmPassword->text() == 0)
{
printf("Invalid password\n");
return;
}

if (ui.RClientLineEdit->text() == 0)
{
printf("Invalid password\n");
return;
}


query.exec(strQuery);

strQuery="SELECT * from Clientwhere DeviceID='%1'";

if (ui.RClientLineEdit->text() != "DeviceID")
{
printf("Invalid DeviceID\n");
return;
}

query.exec(strQuery);



Thank you in advance! :)

Lesiok
28th February 2011, 15:28
First query is incomplete. If You use placeholder :dID You must bind him value.
Second query returns ALL records with DeviceID ending with digit 1.

fantom
28th February 2011, 15:54
Thank you for your response! You are right i forgot to bind the value.

Concerning the second query could you please tell me how i have to write it in order to take the first value of the Client table?

Lesiok
28th February 2011, 17:00
Thank you for your response! You are right i forgot to bind the value.

Concerning the second query could you please tell me how i have to write it in order to take the first value of the Client table?

Try this SQL :

SELECT * FROM Clientwhere ORDER BY here_def_of_sort LIMIT 1but LIMIT is an extension of SQL language and in different databases it has different construction. This is correct for PostgreSQL and MySQL in example. For DB2 it is
SELECT * FROM Clientwhere ORDER BY here_def_of_sort FETCH FIRST 1 ROWS ONLY.