On Sunday 27 February 2005 16:10, Jorge Godoy wrote:
> Hans-Peter Jansen <[EMAIL PROTECTED]> writes:
> > You may try to reproduce this behaviour within a minimal Qt
> > example, since I would suspect this problem there.. PyQts cover
> > is just to thin to mangle the results in such a way ;-)
>
> I'll try and see if I remember enough C++ for that (hey! it's been
> more than 5 years that I don't write C or C++ code, thanks to Perl
> and Python ;-))

Just mangle one of the SQL examples, that comes with Qt...

> > Anyway, I would go the pythonic way by using %s or in more
> > complex situations dictionary referencing: %(senha)s with python
> > strings..
>
> The problem here is loosing the "auto-quoting" facility.  I'd have
> to check the string to see if there's no single quote inside it or
> anything else that can be used to trick the DB and do something
> that is not allowed.
>
> Letting the driver quote everything I don't have to worry with
> that.  At least, everything used to be correct with both pypgsql
> and psycopg when I used to use wxPython for this application.
>
> OR, did I misunderstand you and PyQt will auto-quote things
> correctly if I use the DB API syntax?

You can't use the Python DB API syntax, since it's completely bypassed 
with Qt SQL. I was talking about the usual Python string syntax, but 
you're right, you have to handle the quoting yourself in this case.

BTW, I just checked your problem with the attached script, MySQL, and 
a database as defined as in testdb_mysql.sql from PyQt/examples3/SQL 
without such problems.

Python version: 2.3+
sip version: 4.2
Qt version: 3.2.1
PyQt version: 3.14
MySQL version: 4.0.21

Cheers,
Pete
import sys
from qt import *
from qtsql import *

def create_connection(server, database, user, passw, driver = "QMYSQL3"):
    db = QSqlDatabase.addDatabase(driver)
    if db:
        db.setHostName(server)
        db.setDatabaseName(database)
        db.setUserName(user)
        db.setPassword(passw)
        if db.open():
            return db
        else:
            QMessageBox.warning(None, "Database Error",
                "Cannot open %s database on %s!\n\n%s\n%s\n" %
                (database, server,
                db.lastError().driverText(),
                db.lastError().databaseText()), " Ooops ")
        return None

if __name__ == "__main__":
    a = QApplication([])
    if len(sys.argv) < 5:
        print "usage %s server database user pass [driver]" % sys.argv[0]
        print "available drivers: ", map(str, QSqlDatabase.drivers())
        sys.exit(1)

    server, database, user, passw = sys.argv[1:5]
    driver = "QMYSQL3"
    if len(sys.argv) == 6:
        driver = sys.argv[5]

    if create_connection(server, database, user, passw, driver):
        q = QSqlQuery()
        q.prepare("INSERT INTO staff (forename, surname, salary, statusid)"
                  "VALUES (:forename, :surname, :salary, :statusid)")
        q.bindValue(":forename", QVariant("Bart"))
        q.bindValue(":surname", QVariant("Simpson"))
        q.bindValue(":salary", QVariant(-69999.96))
        q.bindValue(":statusid", QVariant(3))
        if q.execQuery():
            print "Query successful"
        else:
            print "Query failed"


_______________________________________________
PyKDE mailing list    [email protected]
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde

Reply via email to