Results 1 to 13 of 13

Thread: Correct string syntax

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Correct string syntax

    I am trying to use a SQLite UPDATE query and not sure of the proper syntax. I have tried a few differnt way but none works quite right. The issue is trying to include the single quote marks for the update query. I have 1 line edit and one combobox widget that contain the data for part of the string. Here is my last try:
    Qt Code:
    1. Sqlstring=("UPDATE address set LName= '%1' WHERE id= %2")
    2. .arg(ui->txtLName->text()).arg(ui->cmbName->itemData(Index).toString());
    To copy to clipboard, switch view to plain text mode 
    I get the error messeage:
    request for member 'arg' in (UPDATE rider set LName= \'%1'\ WHERE id= %2) which is of non class type const char [24].

    What would be the best method to create the correct string keeping in mind that the ui->txtLName->text() needs to be enclosed ina single quotation mark?

    Thanks a bunch!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Correct string syntax

    That should be:

    Qt Code:
    1. QString("%1").arg(something)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct string syntax

    I don't quite understand.... Sqlstring is declared as aQString. But how do I include the widgets and include one in a single quote?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Correct string syntax

    Learn about implicit conversion

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Correct string syntax

    ...and you might want to escape the string values before using it with a sql statement.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct string syntax

    Is there any specific reason for not using prepared sql statements? Not that it would directly solve the problem here (as it is syntax specific) but would surely be a correct approach to use sql.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct string syntax

    I appreciate all of your help. I am fairly new to QT and C++ but I am trying to learn. I didn't understand on escaping the string values before using them? I looked up implicit conversion but there were so many types that I wasn't sure what they meant. I think I am using a prepared sql statement. My code is:
    Qt Code:
    1. bool MainWindow::save()
    2. {
    3. QString Sqlstring;
    4. QSqlQuery query ;
    5. Sqlstring=("UPDATE address set LName= '%1' WHERE id= %2")
    6. .arg(ui->txtLName->text()).arg(ui->cmbName->itemData(Index).toString());
    7.  
    8. qDebug() << query.lastError();
    9. query.prepare(Sqlstring);
    10. query.exec();
    11.  
    12. return true;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Any links to what you are describing or hints to the correct code would be appreciated!
    Thanks for all your input!

    Rob

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct string syntax

    This is a correctly prepared statement:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE address set LName=:name WHERE id=:id");
    3. query.bindValue(":name", ui->txtLName->text());
    4. query.bindValue(":id", ui->cmbName->itemData(Index).toInt());
    5. if(!query.exec()){
    6. // error
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct string syntax

    Thanks alot!
    Does this also escape the string values as Lykurg suggested? Is this the implicit conversion that tbscope was talking about? I want to research these issues to better understand them and get a better knowelege!

    Thanks
    Rob

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct string syntax

    Quote Originally Posted by poporacer View Post
    Does this also escape the string values as Lykurg suggested?
    Yes.
    Is this the implicit conversion that tbscope was talking about?
    No.

    I want to research these issues to better understand them and get a better knowelege!
    Good luck.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Correct string syntax

    So what would be the implicit version? Which way is better?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct string syntax

    The "implicit" thing is about casting between types in C++. It has nothing to do with SQL.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Correct string syntax

    Quote Originally Posted by poporacer View Post
    So what would be the implicit version?
    Learn the difference between:
    Qt Code:
    1. QString string = "Hello world!";
    2. QString string("Hello world!");
    3. QString string = QString("Hello world!");
    4. QString string2(string);
    5. ...
    To copy to clipboard, switch view to plain text mode 

    Which are initialisations, which are assignments, which are explicit, which are implicit, which use a copy constructor, ...?

    This error tells you everything:
    request for member 'arg' in (UPDATE rider set LName= \'%1'\ WHERE id= %2) which is of non class type const char [24].
    You are requesting a member of an object which is not even a class.

    You did:
    Qt Code:
    1. const char *string = "UPDATE address set LName= '%1' WHERE id= %2".arg(...).arg(...);
    To copy to clipboard, switch view to plain text mode 
    There's no QString there.

Similar Threads

  1. Regarding syntax
    By Yayati.Ekbote in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2010, 14:15
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  3. Need help getting QAbstractItemView syntax correct
    By Hookem in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2008, 09:34
  4. Odd Syntax
    By acxdotfm in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2008, 20:23
  5. StyleSheet syntax checking
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th June 2008, 10:09

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
  •  
Qt is a trademark of The Qt Company.