AAAAHH !!! It always happens! Sit with the problem for one day, finally, decide to publish and ask about it, and about 30 minutes after you publish your problem, you will find a solution!
For those who are interested, I had the same problem as this guy .
Basically, you should create a QApplication instance BEFORE your sql code executes ... ie
# this little monkey has to be here app = QApplication(sys.argv) # rest of the code db = QSqlDatabase.addDatabase("QMYSQL") db.setHostName ( db_host ) db.setUserName ( db_user ) db.setPassword ( db_passwd ) db.setDatabaseName ( db_db ) db.setPort ( db_port ) db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1") db.open() defaultDB = QSqlDatabase.database() query = QSqlQuery("SELECT * FROM Users") qe = query.exec_() print "query exec" , query.exec_() if not qe: # if error print QSqlQuery.lastError( query ).text() else: # else display returned values while query.next(): print "query value" , query.value(0).toString() db.close()
source share