-3

I'm beginner in qt. I'm using qt5 in ubuntu. I want to select some columns from a table in mysql database where one column(id) is equal to a variable in integer type. What should I do? I wrote this code but I have error!

QSqlQuery query;
int k=10;
query.exec("select name,family from employees where id='"+k+"'");
while(query.next()){
    qDebug()<<query.value(0).toString();
    qDebug()<<query.value(1).toString();
}

And this is the error:

 invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
 query.exec("select name,family from employees where id='"+k+"'");
Alex
  • 16,739
  • 1
  • 28
  • 51

1 Answers1

2

I am not familiar with Qt, but simple google search return :

https://stackoverflow.com/a/15902962/4421474

So in your case it could be:

QSqlQuery query;
query.prepare(QString("select name,family from employees where id = :id"));
query.bindValue(":id", k);
query.exec();

By the way you have typo "selecr must be "select.

Community
  • 1
  • 1
Alex
  • 16,739
  • 1
  • 28
  • 51