QString QSqlQuery bug 2

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 254
Joined: Thu Jun 11 2020 12:18 pm

QString QSqlQuery bug 2

Post by seasoned_geek »

Possibly related to the prepare bug. Ripped my hair out for hours on this one.

Code: Select all

void DefaultSyntax::generateSyntaxMasterFromLexers( QSqlDatabase &db )
{
    QSqlQuery query( db );
    unsigned int lexerCnt = GetLexerCount();
    char lexName[2048];

    qDebug() << "lexerCnt: " << lexerCnt << "\n";

    for ( unsigned int jjj=0; jjj<lexerCnt; jjj++ )
    {
        memset( lexName, '\0', sizeof( lexName ) );
        GetLexerName( jjj, lexName, sizeof( lexName )-1 );
        QString cmd = "INSERT INTO SYNTAX_MASTER (SYN_NAME, RANK) VALUES ( \"%1\", 1);";

        cmd = QStringParser::formatArg( cmd, lexName );
        qDebug() << "cmd: " << cmd << "\n";

        if ( !query.exec( cmd ) )
        {
            qDebug() << query.lastError().text();
            qDebug() << query.lastQuery();
        }
    }
}
yields the following

Code: Select all

lexerCnt:  132 

cmd:  INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'a68k'); 

unrecognized token: "'a68k" Unable to execute statement
INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'a68k');
cmd:  INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'abaqus'); 

unrecognized token: "'abaqus" Unable to execute statement
INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'abaqus');
cmd:  INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'ada'); 

unrecognized token: "'ada" Unable to execute statement
INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'ada');
cmd:  INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( 'apdl'); 
The "fix" was as follows:

Code: Select all

        QString lexStr = QString::fromLatin1( lexName );
        QString cmd = "INSERT INTO SYNTAX_MASTER(SYN_NAME) VALUES( '%1');";

        cmd = QStringParser::formatArg( cmd, lexStr );
        qDebug() << "cmd: " << cmd << "\n";


QString gladly took the char buffer and everything display wise handled it just fine. QSqlQuery just gagged on it though.
ansel
Posts: 153
Joined: Fri Apr 10 2015 8:23 am

Re: QString QSqlQuery bug 2

Post by ansel »

This is an interesting issue and is not related to the QSqlQuery problem you reported nor is it a QStringParser bug. The problem has to do with lexName, which you declared as an array of characters.

Here is the line of code to look at: "QStringParser::formatArg(cmd, lexName)". The second parameter will automatically be converted from an array of characters to the data type of cmd, which is of course a QString.

In this case, lexName will be interpreted as a string containing 2048 characters, most of which will be null. The conversion process does not stop at the first null terminator. If you print the contents of this string it may look like what you expect, since the nulls are not printed. However, if you print the size of the string it will be much larger than you expected.

As you discovered, the QString::fromLatin1() method stops at the first null character and returns a much shorter string. You could also have used QString::fromUtf8(). If lexName needs to be an array of characters then using one of these two methods is the correct way to convert the data.
Ansel Sermersheim
CopperSpice Cofounder
Post Reply