Results 1 to 13 of 13

Thread: Insertion double quote into string/qstring

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Insertion double quote into string/qstring

    Hello, I have this code:
    Qt Code:
    1. // query string;
    2. QString queryString("SELECT * FROM merchandize WHERE PicPath="%1";");
    3. queryString.arg(strPicPath);
    To copy to clipboard, switch view to plain text mode 

    Once I try to compile it, I get following error:
    Qt Code:
    1. CMerchandizeBrowser.cpp: In member function `QString CMerchandizeBrowser::getMerchandizeName(QString)':
    2. CMerchandizeBrowser.cpp:330: error: invalid operands of types `const char[41]' and `int' to binary `operator%'
    To copy to clipboard, switch view to plain text mode 

    I need those quotes because SQL syntax. Please help!
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    Ceaser88 (3rd October 2011), MarkoSan (14th December 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Insertion double quote into string/qstring

    Ok, but code:
    Qt Code:
    1. Q_ASSERT(strPicPath.size()>0); // assertion test //
    2. query string;
    3. QString queryString("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
    4. QueryString.arg(strPicPath);
    5. qDebug() << "queryString: " << queryString; // debug
    To copy to clipboard, switch view to plain text mode 
    in the last line (qDebug) reports:
    Qt Code:
    1. queryString: "SELECT * FROM merchandize WHERE PicPath="%1";"
    To copy to clipboard, switch view to plain text mode 
    which is not ok, instead of '%1' there should be the contens of paramaterer strPicPath. I do not get it.
    Qt 5.3 Opensource & Creator 3.1.2

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    QString::arg() returns a copy of the string where the replacements have been done.
    J-P Nurmi

  6. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Insertion double quote into string/qstring

    So, it is ok then?
    Qt 5.3 Opensource & Creator 3.1.2

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    Are you using the return value of QString::arg()? It's an ordinary function which returns a value.
    J-P Nurmi

  8. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Insertion double quote into string/qstring

    Yep, I do. Let me give you complete code:
    Qt Code:
    1. QString CMerchandizeBrowser::getMerchandizeName(QString strPicPath)
    2. {
    3. Q_ASSERT(strPicPath.size()>0); // assertion test
    4. // query string;
    5. QString queryString("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
    6. queryString.arg(strPicPath);
    7. qDebug() << "queryString: " << queryString; // debug
    8.  
    9. QSqlQuery query; // sql query
    10. query.exec(queryString); // executes query
    11. Q_ASSERT(query.value(iMerchandizeFieldNAME).toString().size()>0); // assertion test
    12. qDebug() << query.value(iMerchandizeFieldNAME).toString(); // debug
    13. return(query.value(iMerchandizeFieldNAME).toString()); // returns merchandize name
    14. }
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    Quote Originally Posted by MarkoSan View Post
    Yep, I do. Let me give you complete code:
    Qt Code:
    1. [...]
    2. queryString.arg(strPicPath);
    3. [...]
    To copy to clipboard, switch view to plain text mode 
    To me it looks you don't. Notice that QString::arg() is a const method. Again, it does not modify the original value but returns a modified value.
    J-P Nurmi

  10. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Insertion double quote into string/qstring

    I have this system everywhere in project and it works fine, just here it does not work. So, how should I rewrite the code, have you any clues?
    Qt 5.3 Opensource & Creator 3.1.2

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    Quote Originally Posted by MarkoSan View Post
    I have this system everywhere in project and it works fine, just here it does not work. So, how should I rewrite the code, have you any clues?
    Give yourself a moment to think it. Consider the following piece of pseudo code:
    Qt Code:
    1. QString QString::arg(value) const
    2. {
    3. // does not modify _this_ but returns a copy
    4. QString copy(*this);
    5. copy.replacePlaceHolderWithValue(value);
    6. return copy;
    7. }
    To copy to clipboard, switch view to plain text mode 

    What's the content of foo?
    Qt Code:
    1. QString foo("%1");
    2. foo.arg(1234);
    To copy to clipboard, switch view to plain text mode 

    What's the content of bar?
    Qt Code:
    1. QString bar("%1");
    2. bar = bar.arg(5678);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  12. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Insertion double quote into string/qstring

    foo has 1234

    and bar has 5678

    Am I right?
    Qt 5.3 Opensource & Creator 3.1.2

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insertion double quote into string/qstring

    Quote Originally Posted by MarkoSan View Post
    foo has 1234

    and bar has 5678

    Am I right?
    Unfortunately not (foo has "%1" and bar has "5678"). However, did you notice the difference? Looks subtle, but is in fact significant.
    J-P Nurmi

  14. The following user says thank you to jpn for this useful post:

    MarkoSan (14th December 2007)

  15. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Smile Re: Insertion double quote into string/qstring

    Aha, I must use = operator to set right value. God damn, thanks.
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 15:12
  2. double record insertion
    By tranfuga25s in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2006, 18:12

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.