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.

Advertisements
Standard
Django, Linux, Postgres, Python

Installing Django 1.2.3 on Ubuntu 10.04 & PostgreSQL

Installing Postgres and Django

 sudo apt-get install postgresql pgadmin3 python-psycopg2
apt-get install python-setuptools
easy_install django 

Configuring Postgres

sudo su
passwd postgres
su postgres
psql template1

The last instruction should open the psql shell, where you can run the following:

ALTER USER postgres WITH ENCRYPTED PASSWORD 'mypassword';
Standard