0

I get result from db by selectall query and I want save result in array and send it by socket.

db.open();

QSqlQuery *selectall = new QSqlQuery(db);
selectall->prepare("select * from phone_table");
selectall->exec();
selectall->first();
QString result;
QByteArray arrayresult;
int index = 0;
while (selectall->next())
{
    index += 1;
    // qint16 id = selectall->value(0).toString();
    QString name_ = selectall->value(1).toString();
    QString surname = selectall->value(2).toString();
    QString phone_number = selectall->value(3).toString();
    result = "*"+ name_+"*"+surname+"*"+phone_number;
    arrayresult[index] = result;
}

I get this error binary '=' : no operator found which takes a right-hand operand of type 'const char [16]'

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
developer
  • 37
  • 1
  • 6

3 Answers3

0

You are trying to set a QByteRef to a QString.

I think you may want a QList and to arrayresult.append(result). Or else if you want one QByteArray with the concat of all results use arrayresult+= result.

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
0

You may build the QString you want to initialize QByteArray. To then convert from QString to QByteArray, you can do

QByteArray array_ = string_.toLatin1();

if encoding is Latin1.

You may alternatively use append

QByteArray & QByteArray::append ( const QString & str )

This is an overloaded function.

Appends the string str to this byte array. The Unicode data is converted into 8-bit characters using QString::toAscii().

If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to const char *.

append is doing the same as + operator.

kiriloff
  • 25,609
  • 37
  • 148
  • 229
0

You can do the following with the toLatin1() function of the QString.

// ...

QString result = QString( "*%1*%2*%3" ).arg( name_ )
                                       .arg( surname )
                                       .arg( phone_number );

QByteArray resultArray = result.toLatin1();
// Or ...
// QByteArray resultArray = result.toLocal8Bit();
// QByteArray resultArray = result.toUtf8();

And you shall use a QList< QByteArray > for containing the results, or you can just append the last result item to your final result object.

p.i.g.
  • 2,815
  • 2
  • 24
  • 41