Results 1 to 12 of 12

Thread: QSqlQuery problem

  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 QSqlQuery problem

    Hi, I have the code:
    Qt Code:
    1. QString CMerchandizeBrowser::getMerchandizeName(QString strPicPath)
    2. {
    3. Q_ASSERT(strPicPath.size()>0); // assertion test
    4. // use query
    5. /*
    6.   QSqlQuery useQuery("USE merchandize;");
    7.   qDebug() << useQuery.lastError(); // debug
    8. */
    9. // query string;
    10. QString queryString("SELECT * FROM `eros`.`merchandize` WHERE PicPath=\"%1\";");
    11. queryString=queryString.arg(strPicPath);
    12. qDebug() << "queryString:" << queryString; // debug
    13. QSqlQuery query(queryString); // executes sql query
    14. //Q_ASSERT(query.value(iMerchandizeFieldNAME).toString().size()>0); // assertion test
    15. qDebug() << query.lastError(); // debug: shows error
    16. qDebug() << query.value(iMerchandizeFieldNAME).toString(); // debug
    17. return(query.value(iMerchandizeFieldNAME).toString()); // returns merchandize name
    18. }
    To copy to clipboard, switch view to plain text mode 

    and after qDebug() << query.lastError() I get:
    Qt Code:
    1. warning: QSqlError(-1, "", "")
    To copy to clipboard, switch view to plain text mode 

    The connection to database is ok, I've double checked. What is wrong, please help!
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    I don't think you need ";" appended to the query string. I think it is added automatically.
    Remove it and see if there's any difference.

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

    Angry Re: QSqlQuery problem

    Same result. And in both cases, I also get following warning in debugging process:
    Qt Code:
    1. QSqlQuery::value: not positioned on a valid record
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Everything is OK with the query, so the problem must be somewhere else. Double check you database connection... What happens if you pass the database as the second parameter to QSqlQuery.

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

    MarkoSan (15th December 2007)

  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: QSqlQuery problem

    God damn, upon seinding paramaters to database, as 3rd paramater I've sent hostname instead of database name and normally, database was not activated. Marcel, thanks!!!!

    But on the other hand, I have one more question. I have another function that retreives data from another table and it has been working perfectly before I've corrected this glitch with database paramaters. How is that possible?
    Qt 5.3 Opensource & Creator 3.1.2

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    I don't know... Magic?
    Maybe that database was the default schema for the user you connected with...

  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

    Default Re: QSqlQuery problem

    I will dig into this, it'a possible nasty bug, and let me report what I've discovered.
    Qt 5.3 Opensource & Creator 3.1.2

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by MarkoSan View Post
    warning: QSqlError(-1, "", "")
    This actually means that there is no error.

    Quote Originally Posted by MarkoSan View Post
    QSqlQuery::value: not positioned on a valid record
    You don't invoke QSqlQuery::next() before value().

    Also better use bindValue() to avoid SQL injection:
    Qt Code:
    1. q.prepare( "SELECT * FROM `eros`.`merchandize` WHERE PicPath= :path" );
    2. q.bindValue( ":path", strPicPath );
    3. if( q.exec() ) {
    4. while( q.next() ) {
    5. // ...
    6. }
    7. }
    8. else {
    9. // error
    10. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Sep 2007
    Posts
    31
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QSqlQuery problem

    I have another big problem with QSqlQuery.
    I'm binding values using bindValue() function. Inside exec() function driver()->formatValue() function is called for each binded value, so each '\' becames '\\'.
    Is theer any way to avoid such behaviour?

    (I have standart_conforming_strings = on in my postgres.conf so I don't need escaping '\' characters)

  11. #10
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Hi,
    Try to use this style

    GetQry = ( char *) malloc ( ( 50 * sizeof ( char ) ) + 1 );

    strcpy(GetQry, "select * from TableList where id > ");
    TmpQry = ( char * ) realloc ( GetQry, sizeof( char ) * ( strlen(GetQry ) + strlen(TID) + 1 ) );

    GetQry = TmpQry;
    strcat(GetQry, TID);

    and execute that string GetQry


  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by sabeesh View Post
    GetQry = ( char *) malloc ( ( 50 * sizeof ( char ) ) + 1 );

    strcpy(GetQry, "select * from TableList where id > ");
    TmpQry = ( char * ) realloc ( GetQry, sizeof( char ) * ( strlen(GetQry ) + strlen(TID) + 1 ) );

    GetQry = TmpQry;
    strcat(GetQry, TID);
    Don't even dare to write such horrible things in a Qt-based code.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by brokensword View Post
    I have another big problem with QSqlQuery.
    I'm binding values using bindValue() function. Inside exec() function driver()->formatValue() function is called for each binded value, so each '\' becames '\\'.
    Is theer any way to avoid such behaviour?

    (I have standart_conforming_strings = on in my postgres.conf so I don't need escaping '\' characters)
    I think you should contact the Trolls as this is something that should be fixed in Qt code before standard_conforming_strings becomes "on" by default.

Similar Threads

  1. QSqlQuery error
    By skuda in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 08:43
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.