PyQt, Python

Connecting to MySQL using PyQt

The first thing to do in getting the database connection to your application is to install sql drivers for python. To do this you’ll need to do the following:

# sudo apt-get install python-qt4-sql

This should install the necessary libraries. To test it (which is also a demonstration of how database connections in applications are made) you’ll need to do this;

Open Python Console

# python

>>> from PyQt4.QtSql import *

>>> db = QSqlDatabase.addDatabase(“QMYSQL”)

>>> db.setDatabaseName(“information_schema”)

>>> db.setUserName(“root”)

>>> db.setPassword(“obama”)

>>> db.open()

True

>>> query = QSqlQuery();

>>> query.exec_(“SELECT COUNT(*) FROM TABLES;”);

True

>>> query.next()

True

>>> query.value(0).toInt();

(205, True)

You can try out a more complex query using your own database. This is not necessarily the best method just a little overview on how to do it.

Standard

5 thoughts on “Connecting to MySQL using PyQt

  1. i want to believe the database is just meant to store data and security is the least of worry here. such as for download manager’s download history, remote file backup history etc. if that is the case, this is way better compared to mysqldb as this integrates well with qt itself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s