[PyQt] QSqlQuery: one parameter in two different places?

Sibylle Koczian Sibylle.Koczian at t-online.de
Wed Jan 2 14:49:14 GMT 2008


Am Dienstag, 1. Januar 2008 23:22:38 schrieb Hans-Peter Jansen:
>
> On debugging such problems, I found QSqlQuery.lastQuery() pretty useful
> (before digging in the Qt source..). Hopefully it still exists in Qt4.
> Applied to your problem, let us know about the outcome.
>

QSqlQuery.lastQuery() still exists, there is also QSqlQuery.executedQuery(). 
Applied to my problem they give:

1. One parameter name in both places:

def abrechnung(datum):
    sqltext = """UPDATE ausgaben SET abrech_datum = :a_datum
    WHERE abrech_datum IS NULL AND ausg_datum < :a_datum"""
    query = QSqlQuery()
    query.prepare(sqltext)
    query.bindValue('a_datum', QVariant(datum))
    query.exec_()
    print 'numRowsAffected:', query.numRowsAffected()
    print 'lastQuery:', query.lastQuery()
    print 'executedQuery:', query.executedQuery()

result:
numRowsAffected: -1 (in fact nothing was updated)
lastQuery: UPDATE ausgaben SET abrech_datum = :a_datum
    WHERE abrech_datum IS NULL AND ausg_datum < :a_datum
executedQuery: UPDATE ausgaben SET abrech_datum = ?
    WHERE abrech_datum IS NULL AND ausg_datum < ?

Why the '?'? 

2. Two names for the same parameter:

def abrechnung(datum):
    sqltext = """UPDATE ausgaben SET abrech_datum = :a_datum
    WHERE abrech_datum IS NULL AND ausg_datum < :b_datum"""
    query = QSqlQuery()
    query.prepare(sqltext)
    query.bindValue('a_datum', QVariant(datum))
    query.bindValue('b_datum', QVariant(datum))
    query.exec_()
    print 'lastQuery:',
    print query.lastQuery()
    print 'executedQuery:',
    print query.executedQuery()
    
result:
numRowsAffected: 2 (which is correct)
lastQuery: UPDATE ausgaben SET abrech_datum = :a_datum
    WHERE abrech_datum IS NULL AND ausg_datum < :b_datum
executedQuery: UPDATE ausgaben SET abrech_datum = ?
    WHERE abrech_datum IS NULL AND ausg_datum < ?

Again '?' in place of values for the parameters, but this time it works.

Should I send a little more code to make a complete example? Or does this tell 
enough about the problem?

Thank you,
Sibylle

-- 
Dr. Sibylle Koczian


More information about the PyQt mailing list