Results 1 to 10 of 10

Thread: Problem with QSqlQuery and the .arg of a QString[SOLVED]

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Problem with QSqlQuery and the .arg of a QString[SOLVED]

    Hello,

    The next code works perfectly:

    Qt Code:
    1. //Note: CSQLUsers::AL_PUPIL is "1", an intenger
    2.  
    3. QSqlQuery data(QString("SELECT id FROM users WHERE iaccesslevel=%1 AND sname = 'Alex' ") .arg(CSQLUsers::AL_PUPIL), *pointer_my_database->GetDBPtr());
    4.  
    5. bool test1 = data.first();
    6. bool test2 = data.isValid();
    7. bool test3 = data.isSelect();
    To copy to clipboard, switch view to plain text mode 

    But, when I try to use the arguments of QString (something that works with integers or other sort of variables)

    Qt Code:
    1. //Note: CSQLUsers::AL_PUPIL is "1", an intenger
    2.  
    3. QString name = 'Alex';
    4. QSqlQuery data(QString("SELECT id FROM users WHERE iaccesslevel=%1 AND sname = %2 ") .arg(CSQLUsers::AL_PUPIL) .arg(name), *pointer_my_database->GetDBPtr());
    5.  
    6. bool test1 = data.first();
    7. bool test2 = data.isValid();
    8. bool test3 = data.isSelect();
    To copy to clipboard, switch view to plain text mode 

    the three booleans are false... why? the argument should be converted to QString properly and call the SQL :S

    thanks!
    Last edited by jano_alex_es; 5th May 2009 at 11:13. Reason: add "[solved]"

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

    Default Re: Problem with QSqlQuery and the .arg of a QString

    It is but it is not quoted so the select statement is invalid. You should use QSqlQuery::prepare and QSqlQuery::bindValue instead of substituting arguments directly.
    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.


  3. The following user says thank you to wysota for this useful post:

    jano_alex_es (5th May 2009)

  4. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and the .arg of a QString

    The single quote around the name is missing in the second code block. Try this

    Qt Code:
    1. //Note: CSQLUsers::AL_PUPIL is "1", an intenger
    2.  
    3. QString name = 'Alex';
    4. QSqlQuery data(QString("SELECT id FROM users WHERE iaccesslevel=%1 AND sname = '%2' ") .arg(CSQLUsers::AL_PUPIL) .arg(name), *pointer_my_database->GetDBPtr());
    5.  
    6. bool test1 = data.first();
    7. bool test2 = data.isValid();
    8. bool test3 = data.isSelect();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to munna for this useful post:

    jano_alex_es (5th May 2009)

  6. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and the .arg of a QString

    Try something like this (You must write %2 in apostrophes)
    Qt Code:
    1. QString name = 'Alex';
    2. QSqlQuery data(QString("SELECT id FROM users WHERE iaccesslevel=%1 AND sname = '%2' ") .arg(CSQLUsers::AL_PUPIL) .arg(name), *pointer_my_database->GetDBPtr());
    To copy to clipboard, switch view to plain text mode 

    or this
    Qt Code:
    1. QString name = 'Alex';
    2. QSqlQuery data(QString("SELECT id FROM users WHERE iaccesslevel=%1 AND sname = :sname ") .arg(CSQLUsers::AL_PUPIL), *pointer_my_database->GetDBPtr());
    3. data.bindValue(":sname",name);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lesiok for this useful post:

    jano_alex_es (5th May 2009)

  8. #5
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QSqlQuery and the .arg of a QString

    thanks, but it's not working :S

    If I use the '%2' solution,

    data.first(); == false
    data.isValid() == false
    data.isSelect(); == true

    but if I use the data.bindValue, the three are set to false :S

    when I use "name = 'Alex'" the three are set to true, so in my opinion the problem is not in the database

  9. #6
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QSqlQuery and the .arg of a QString

    On the other hand, the next code:

    Qt Code:
    1. QString name = 'Alex';
    2. QSqlQuery data(*pointer_my_database->GetDBPtr());
    3. data.prepare(QString("SELECT id FROM users WHERE iaccesslevel=:iaccesslevel AND sname = :sname"));
    4.  
    5. data.bindValue(":sname", name);
    6. data.bindValue(":iaccesslevel", CSQLUsers::AL_PUPIL);
    7. data.exec();
    8.  
    9. bool test1 = data.first();
    10. bool test2 = data.isValid();
    11. bool test3 = data.isSelect();
    To copy to clipboard, switch view to plain text mode 

    gives me false, false and true again :S

  10. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and the .arg of a QString

    Why You have

    Qt Code:
    1. QString name = 'Alex';
    To copy to clipboard, switch view to plain text mode 

    not

    Qt Code:
    1. QString name = "Alex";
    To copy to clipboard, switch view to plain text mode 

    ???

  11. The following user says thank you to Lesiok for this useful post:

    jano_alex_es (5th May 2009)

  12. #8
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QSqlQuery and the .arg of a QString

    Because I was testing, testing, testing and testing, and I though "maybe the problem is in the double-mark, SQL uses simple so let's try". The compiler said nothing, I though it was the same.

    Thanks, definitly your solutions were right and the last problem was that.

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

    Default Re: Problem with QSqlQuery and the .arg of a QString

    Quote Originally Posted by jano_alex_es View Post
    The compiler said nothing, I though it was the same.
    I see someone is using a lousy... eee.... Microsoft compiler.
    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.


  14. #10
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem with QSqlQuery and the .arg of a QString[SOLVED]

    what can I say, the company chooses the compiler :P

Tags for this Thread

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.