Does QSql connect and read from a sample database? - Driver not loaded

I recently started figuring out the need for a change from __mysql to PyQt QSql, but you have nooo idea where to start. All I want to do (for now) is read from the database and prints the results. This is the further result that I got, but I keep getting the "Driver not loaded Driver not loaded" error returned by the query.exec_ () function.

Please, help!

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() 
+4
source share
1 answer

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() 
+7
source

All Articles