1

My question is similar to this one: link except that I don't want to serialize the QList. I want to store a QList as a double precision array in the DB table.

I am trying the following bindValue command for the variable val:

query.bindValue( QStrFromSrcEnc( id ), QVariant::fromValue( val ) );

where val is QList and id is ":value"

I am executing the following postgres query:

"INSERT INTO valtable( type_id, asset_id, value, tag )\n"
            "VALUES ( :typeId, :assetId, :value, :tag )\n"

The value column of the table is of the type double precision[].

I get the following error, as you can see the 3rd field for value is blank which means val wasn't bound correctly.

ERROR: syntax error at or near ","
LINE 1: EXECUTE qpsqlpstmt_13e (60, 63, , '')
                                        ^

however, if val is simply a QVariant, then it works fine.

Community
  • 1
  • 1
Lyman Zerga
  • 1,415
  • 3
  • 19
  • 40

1 Answers1

3

Ok, I did a workaround. I convert the QList List to a QString in a postgres array format (e.g.'{1.32,4.43}').

QString valueStr = QStrFromSrcEnc("{");

for(int i=0; i<List.size(); ++i)
{

    valueStr += QString::number( List[i] );
    valueStr += QStrFromSrcEnc(",");
}

valueStr.chop(1);
valueStr += QStrFromSrcEnc("}");

then I bind the string value to the :val placeholder

Lyman Zerga
  • 1,415
  • 3
  • 19
  • 40