[PyQt] sql problems of a n00b

Volker Helm Helm.volker at gmx.de
Thu Sep 3 21:49:43 BST 2009


If I understand you correctly, you're going to do something like this:

try:
    a=int(self.cautat.text())
    print 'a=%i' % a
    self.query.prepare('select * from pacienti where cnp = ?')
    self.query.addBindValue(QtCore.QVariant(a))
except:
    self.query.prepare('select * from pacienti where nume=?')
    self.query.addBindValue(QtCore.QVariant\
                 (QtCore.QString(self.cautat.text())))
self.query.exec_()

It would be safer to do something like this:

if self.cautat.text().isdigit == True:
    self.query.prepare('select * from pacienti where cnp = ?')
    self.query.addBindValue(QtCore.QVariant(int(self.cautat.text())))
else:
    self.query.prepare('select * from pacienti where nume=?')
    self.query.addBindValue(QtCore.QVariant\
                 (QtCore.QString(self.cautat.text())))
self.query.exec_()


There may be the problem that self.cautat.text() contains a utf8 coding. I recently stombled over this problem solution would be (if I remember correctly):

   self.query.addBindValue(QtCore.QVariant\
                 (QtCore.QString(unicode(self.cautat.text()))))


Hope something will help,

Volker
-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser


More information about the PyQt mailing list