Hello,
I often have the problem, that the Qt installation on some computers is not
shipped with the mysql plugin. Hence, I can't access a mysql database from
PyQt on such an computer.
Since I need to have the same compiler, as Qt has been compiled with, to build
the plugin myself, I thought it would be far easier to implement my own
SqlDriver from within python (e.g. with mysql-python).
Therefore I subclassed QSqlDriver and QSqlResult from PyQt. However, if I do
so I get a segmentation fault when I run a query on my database connection.
I stripped the code down to the most simple case which just simulates a
database without data.
As soon as I run a query (QSqlQuery) on this database the python shell crashes
with a segmentation fault.
To figure out why I digged into the qt cpp-source code and followed along the
QSqlQuery::exec(QString query) method. I followed along the lines and could
repeat the calls on my on PySqlResult created with createResult() method.
If I do everything in QSqlQuery::exec(QString) my self it works. By using
QSqlQuery it does not. I wonder if it has to do something with the internal
assignements done in QSqlQuery::exec:
*this = QSqlQuery(...)
Has anybody ever tried to implement a QSqlDriver in python and succeeded? Any
hint for me?
Regards
Andre
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtSql import QSqlDriver, QSqlRecord, QSqlResult, QSqlField, QSqlDriverCreatorBase, QSqlDatabase, QSqlQuery
class PySQLResult(QSqlResult):
def __init__(self, db):
QSqlResult.__init__(self, db)
print "PySQLResult.__init__"
def data(self, index):
return QtCore.QVariant(4)
def fetch(self, index):
self.setAt(index)
return True
def fetchFirst(self):
self.setAt(0)
return True
def fetchLast(self):
self.setAt(self.size()-1)
return True
def isNull(self, index):
return self.data(index).isNull()
def numRowsAffected(self):
return 0
def reset(self, query):
return False
def size(self):
return 0
class PySQLDriver(QSqlDriver):
def __init__(self, parent = None):
QSqlDriver.__init__(self, parent)
print "PySQLDriver.__init__"
self.__features = set([])
self.__conn = None
def hasFeature(self, f):
try:
return f in self.__features
except:
return False
def open(self, database, user, password, host, port, connOpts):
self.__conn = str(host)
return not self.__conn == None
def close(self):
if self.__conn:
self.__conn = None
return True
return False
def createResult(self):
return PySQLResult(self)
def isOpen(self):
return not self.__conn == None
def handle(self):
return self.__conn
class PySQLDriverCreator(QSqlDriverCreatorBase):
def __init__(self):
QSqlDriverCreatorBase.__init__(self)
def createObject(self):
return PySQLDriver()
app = QtGui.QApplication([])
QSqlDatabase.registerSqlDriver('PySQL', PySQLDriverCreator())
db = QSqlDatabase.addDatabase('PySQL')
db.setHostName('localhost')
print "Opening Database:", db.open()
print "Database has correct driver:", db.driver()
print
print "Constructing a resultset is ok."
result = db.driver().createResult()
print "The result has the correct driver:", result.driver()
print
print "Constructing a query is ok and indeed constructs the correct resultset"
query = QSqlQuery()
print "Retrieving the driver from the query gives a segfault."
#print "The driver is:", query.driver()
print
print "Running a sql query gives a segfault."
#query.exec_('select * from table')
print "Deleting the QSqlQuery object gives a segfault:"
del query
print "This is never reached"
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt