[Tutor] Can't get Python connected to databases

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jan 21 18:07:09 EST 2004



On Wed, 21 Jan 2004, Iain wrote:

> I have been trying to get a connection between Python and a back-end
> database:  something that I expected to be a trivial exercise.
> Unfortunately, I seem
>
> to be getting nowhere and could really do with some help.

Hi Iain,

Ok, let's take a look:


> My environment : Python 2.3 (#2, Aug 31 2003, 17:27:29)
> [GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-1mdk)] on linux2
>
> RPMs installed :
> -bash-2.05b$ rpm -qa|grep -iE 'python|mysql|postgres'
> python-base-2.3-3mdk
> libmysql12-4.0.15-1mdk
> MySQL-python2-0.9.2-1
> postgresql-server-7.3.4-2mdk
> libpython2.2-2.2.2-6mdk
> MySQL-client-4.0.15-1mdk
> libpython2.3-2.3-3mdk
> python-2.3-3mdk
> MySQL-4.0.15-1mdk
> postgresql-7.3.4-2mdk
> postgresql-python-7.3.4-2mdk
> MySQL-common-4.0.15-1mdk
> libpython2.3-devel-2.3-3mdk
> postgresql-devel-7.3.4-2mdk


Thanks, that RPM list helps us a lot.


Personally, I've had the best success if I compile MySQLdb, so maybe we
should go that route.  I think you may need to install the MySQL devel
RPM.  I'm not too familiar with Mandrake-Linux, but I'm guessing that the
RPM's name begins with:

    libmysql12-devel

You should be able to find this on your Mandrake CD, or from your friendly
RPM mirror.


> I don't think I have anything compiled from source or in other package
> formats relevant to this.
>
> 1. MySQL
>
> I have tried several versions of the MySQLdb module downloaded from
> sourceforge.net/projects/mysql-python.


Once you have that libmysql12-devel RPM installed, try going the source
route again.  You should get farther with it.


> Other source versions, including the latest test version, fail to
> compile with a host of errors.

Show us the errors when you try this again --- some of us know C well
enough to understand what the build errors are actually trying to say.







> 2. PostgreSQL
>
> I thought I would go for PostgreSQL next because the python module ships with
> Mandrake.  What could go wrong?  The module (pg or _pg) loads up fine.  I
> still can't connect to the datbase though (I created a database called
> "web").
>
> >>> import _pg
> >>> db = _pg.connect('web','localhost')


It looks like you're using the PyGreSQL interface:

    http://developer.postgresql.org/docs/postgres/pygresql.html


Don't use '_pg' unless you really mean to: the leading underscore is a
hint that the module is not meant to be used directly.  You should be
using either the stable 'pg' module, or the more recent 'pgdb' DB-API 2.0
driver.



For the moment, let's use 'pgdb', since it's using the standard DB-API
2.0.  The pgdb.connect() function takes several arguments:

###
Help on function connect in module pgdb:

connect(dsn=None, user=None, password=None, host=None, database=None)
    # connects to a database
###


and I get the feeling we need to use keyword parameters: notice that the
first two arguments here are the "Data Source Name" (dsn) and 'user'
parameters, and not the 'database' and 'user' parameters that you expect.
That's probably what is causing the connection error you were seeing
earlier.


For example, here's what you should see on a successful connect:

###
>>> pgdb.connect(database='pub')
<pgdb.pgdbCnx instance at 0x81c380c>
###

Here, we explictely say that the 'database' argument should be the 'pub'
database.



So try:

###
import pgdb
pgdb.connect(database='web', host='localhost')
###

You should get better results from this.




Hope this helps!




More information about the Tutor mailing list