[Qt-interest] Weird behaviour of QSqlDriver::sqlStatement

Andre Somers andre at familiesomers.nl
Fri Mar 26 10:02:12 CET 2010


Hi,

I am trying to make my application be able to use multiple databases. It 
is a bit of a struggle to be honest; Qt does not make this as easy as 
moving between windows and linux...

To make my queries database-independent, I use QSqlDriver::sqlStatement. 
It allows you to have the driver generate an appropriate sql statement 
for the database that driver is for. At least, that's what I gather, 
since documentation on this is very sparse and it is not used in any SQL 
example. The behaviour I now run into is this:

If I run this code:

    QSqlRecord record;
    QSqlField field(m_id1FieldName);
    field.setValue(1);
    record.append(field);
    QSqlField field2(m_id2FieldName);
    field2.setValue(1);
    record.append(field2);
    QSqlField field3(m_strengthField);
    field3.setValue(1);
    record.append(field3);

    QString insertSql = 
db.driver()->sqlStatement(QSqlDriver::InsertStatement, m_tableName, 
record, true);
    qDebug() << insertSql;

I get the expected result for my database:
"INSERT INTO [JaccardsOnArticleTitles] ([ArticleId_1], [ArticleId_2], 
[Jaccard]) VALUES (?, ?, ?)"

(the square brackets come from my own thin layer, don't worry about 
those, those are correct for use with Ms Access). Note how the values 
are nicely represented as placeholders, as requested by the 'true' as 
the last argument for sqlStatement.

However, if I remove the QField::setValue calls, which would be 
reasonable since these will be replaced with placeholders anyway, the 
result is an empty statement. Nothing at all is returned! While I could 
of course just insert dummy values like I do now, I find this behaviour 
weird. Is there any reason for this? Why isn't this documented? Am I 
missing something here? Note that this only happens if the statement 
type argument is InsertStatement or UpdateStatement. Select statements 
work fine (for the drivers I tested with), and Where statements return a 
statement with an 'Is Null' for the value, which is nice as well.

I can only second Oliver Knoll in the "Why is using XmlPatters so 
cumbersome" thread a little earlier, when he said:

> But I like your ideas very much and I hope the focus of Qt development does not get lost too much on "fancy widgets", but rather goes towards "core functionality" again.

André






More information about the Qt-interest-old mailing list