views:

55

answers:

4

Hi All,

A question about connecting Python To MySQL DB:

How Can I Do That ?!

Link, If You Have References or ...

A: 

Try MySQLdb

Dirk
+1  A: 

Here's a simple example:

import MySQLdb
conn = MySQLdb.connect(host="localhost",
                       user="myusername",
                       passwd="mypassword",
                       db="mydb")
c = conn.cursor()
c.execute("SELECT mycolumn FROM mytable WHERE id = %s;", (1,))
c.fetchone()
c.close()
conn.close()

Note that MySQLdb uses %s as the parameter placeholder.

Adam Bernier
A: 

Why not google it?

The connect() method works nearly the same as with _mysql:

import MySQLdb
db=MySQLdb.connect(passwd="moonpie",db="thangs")

To perform a query, you first need a cursor, and then you can execute queries on it:

c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))
chenge
A: 

If you want higher level functionality, take a look at http://www.sqlalchemy.org/. It's an awesome piece of work.

Rob Cowie