Tim
Tim

Reputation: 57

pyQT and MySQL or MSSQL Connectivity

After much study and investigation, I've decided to do my Python development with pyQT4 using Eric5 as the editor. However, I've run into a brick wall with trying to get MySQL to work. It appears that there's an issue with the QMySQL driver. From the discussions that I've seen so far, the only fix is to install the pyQT SDK and then recompile the MySQL driver. A painful process that I really don't want to have to go through. I would actually prefer to use MS SQL but I'm not finding any drivers for pyQT with MSSQL support.

So, my question is: What is the best approach for using pyQT with either mySQL or MSSQL, that actually works?

While waiting for an answer, I might just tinker with SQLAlchemy and mySQL.Connector to see if it will co-exist with pyQT.

Upvotes: 1

Views: 7303

Answers (3)

TimothyAWiseman
TimothyAWiseman

Reputation: 14873

You might consider pyodbc for your database connectivity, even if the GUI is being provided by QT. Pyodbc is compatible with both MySQL and MS SQL Server.

Upvotes: 0

Gary Hughes
Gary Hughes

Reputation: 4510

If you'd rather use MS SQL it's quite easy in PyQt using QSqlDatabase and the ODBC driver.

Here's an excerpt from a project I'm working on that uses MS SQL.

db = QSqlDatabase.addDatabase('QODBC')
#TODO: Add support for trusted connections.
#("Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=yes;")
db.setDatabaseName('DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s;'
                        % (hostname,
                           databasename,
                           username,
                           password))
db.open()

Obviously you'll need to have already set the hostname, databasename, username and password variables. The commented out line shows the connection string for Trusted Connections; not something I've actually needed to try yet, but it should work if that's what you need.

Once you have the database opened you can use QSqlQuery as you would if using MySql with the PyQt driver.

Upvotes: 5

Mike Ramirez
Mike Ramirez

Reputation: 10970

yes that will work, I do the same thing. I like a programming API, like what SQLAlchemy provides over the Raw SQL version of Qt's QtSql module. It works fine and nice, just populate a subclassed QAbstractTableModel with data from your sqlalchemy queries, like you would with data from any other python object. This though means you're handling caching and database queries, losing the niceness of QtSqlTableModel. But shouldn't be too bad.

Upvotes: 2

Related Questions