Hi All,
A question about connecting Python To MySQL DB:
How Can I Do That ?!
Link, If You Have References or ...
Hi All,
A question about connecting Python To MySQL DB:
How Can I Do That ?!
Link, If You Have References or ...
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.
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,))
If you want higher level functionality, take a look at http://www.sqlalchemy.org/. It's an awesome piece of work.