Results 1 to 1 of 1

Thread: QSqlQuery escaping single quotes

  1. #1
    Join Date
    Jan 2008
    Posts
    107
    Thanks
    36
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb [SOLVED] QSqlQuery escaping single quotes

    Hi,

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

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

    Qt Code:
    1. QString WKT = QString("st_geomfromtext('POINT(%1 %2)', 4326)").arg(lon).arg(lat);
    To copy to clipboard, switch view to plain text mode 

    and getting this in my server's log:
    Qt Code:
    1. 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)')
    To copy to clipboard, switch view to plain text mode 

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

    also tried:
    Qt Code:
    1. QString WKT = QString("st_geomfromtext(\'POINT(%1 %2)\', 4326)").arg(lon).arg(lat);
    To copy to clipboard, switch view to plain text mode 

    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...

    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:

    Qt Code:
    1. QString WKT = QString("POINT(%1 %2)").arg(lon).arg(lat);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. query.prepare("BLAH BLAH BLAH... st_geomfromtext(:etadest, 4326))");
    2. query.bindValue(":etadest", WKT);
    To copy to clipboard, switch view to plain text mode 

    Hope this helps someone..
    Last edited by pdoria; 1st April 2012 at 21:37.

Similar Threads

  1. Replies: 4
    Last Post: 30th August 2020, 19:44
  2. Inserting PNG into Database - Escaping String
    By bhavya in forum Qt Programming
    Replies: 6
    Last Post: 17th March 2011, 15:48
  3. Escaping %1 with QString::arg()
    By marcvanriet in forum Newbie
    Replies: 2
    Last Post: 20th January 2011, 01:38
  4. qmake LIBS missing quotes
    By redoctober0 in forum General Programming
    Replies: 1
    Last Post: 12th January 2011, 16:05
  5. QSqlQuery and single quotes using bindValue
    By Luc4 in forum Qt Programming
    Replies: 4
    Last Post: 26th September 2010, 23:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.