PDA

View Full Version : QSqlQuery escaping single quotes



pdoria
1st April 2012, 21:37
Hi,

Perused the forum's related threads but none gave me a real answer (or one I could process.. :D )

got this query with a call to a Postgis function defined in a QString for later insertion in the prepare() :


QString WKT = QString("st_geomfromtext('POINT(%1 %2)', 4326)").arg(lon).arg(lat);

and getting this in my server's log:

2012-04-01 18:44:31 UTC STATEMENT EXECUTE qpsqlpstmt_af ('2012-04-01 19:44:31', '123456789012345', 161, 513, 18, '2012-04-01 19:47:05', 1361, 'st_geomfromtext("POINT(-16.8516 32.64)'', 4326)')

fx st_geomfromtext failing because it needs single quotes...
single quotes are being replaced with double quotes... :(

also tried:

QString WKT = QString("st_geomfromtext(\'POINT(%1 %2)\', 4326)").arg(lon).arg(lat);

no luck here either...

so what's the correct way of leaving those single quotes there?

Thx in advance! ;)

Added after 1 34 minutes:

ok... replying to self... :rolleyes:

since I was going no where with the previous method ... started to think "out of the box" eheheh

Solution:

in the query.prepare string:
simply call the desired function and then feed it its "single-quote-needed-parameters" like this:


QString WKT = QString("POINT(%1 %2)").arg(lon).arg(lat);


query.prepare("BLAH BLAH BLAH... st_geomfromtext(:etadest, 4326))");
query.bindValue(":etadest", WKT);

Hope this helps someone.. ;)