tags:

views:

108

answers:

3

Is there an easy way (without downloading any plugins) to connect to a MySQL database in Python?

Also, what would be the difference from calling a PHP script to retrieve the data from the database and hand it over to Python and importing one of these third-parties plugins that requires some additional software in the server.

EDIT: the server has PHP and Python installed by default.

A: 

If you don't want to download the python libraries to connect to MySQL, the effective answer is no, not trivially.

synic
+2  A: 

You just need the MySQL for Python module that is Python DB API 2.0 compliant.

I don't know why wouldn't you want to install it. If you are worried about it being too complex to install, there are eggs to make it easy to install.

Once installed, you just use it like

>>> import MySQLdb
>>> db=MySQLdb.connect(host="localhost",user="joebob",
                   passwd="moonpie",db="thangs")

>>> c=db.cursor()
>>> max_price=5
>>> c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))
>>> c.fetchone()
(3L, 2L, 0L)
voyager
I've also used this module and recommend it. Although some users experience problems installing it -- myself included -- any problems you may encounter will likely be solved by a Google search. Don't be intimidated by it, it's worth the effort.
Cal Jacobson
just did "apt-get install python-mysqldb"
Ben
Or you can take a look at setuputils(easy_install) to make installation pretty trivial: http://pypi.python.org/pypi/setuptools
agscala
A: 

No, there is no way that I've ever heard of or can think of to connect to a MySQL database with vanilla python. Just install the MySqldb python package-

You can typically do:

sudo easy_install MySqldb
pyrony