1

A question about connecting Python To MySQL DB:

How Can I Do That ?!

Link, If You Have References or ...

1

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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,))

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.