Hello,

You can specify the port of the instance you want to connect to simply by 
passing "port=NUM" in MySQLdb.connect(). Here is a formulation I have found 
generally useful:

import MySQLdb

def run_mysql(query, db, user, passwd, port=3306, 
socket="/var/run/mysqld/mysqld.sock", host="localhost"):
    """Runs query passed with connection info passed, returns results."""
    try:
        db = MySQLdb.connect(user=user, passwd=passwd, db=db, port=port, 
unix_socket=socket, host=host)
    except MySQLdb.Error, e:
        raise e
    cursor = db.cursor()
    cursor.execute(query)
    data = cursor.fetchall()
    db.close()
    return data


This sets common defaults, but you can of course pass in whatever you want when 
you call it. 

You might also want to check out the MySQLdb docs: 
http://mysql-python.sourceforge.net/MySQLdb.html. It contains a number of 
examples.

_______________________
Samuel Huckins


Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins




________________________________
From: Network Administrator <[email protected]>
To: [email protected]
Sent: Monday, February 9, 2009 1:29:21 PM
Subject: [Tutor] Python & MySQL... Define port.

Hello everybody!


I want to use Python to query a given MySQL database. However, this database is 
running on port 3308, instead the traditional 3306.

I read that (example):

MySQLdb.connect(host="dbserv', db='inv', user='smith')

can be used to query a given database; however, it didn't work in my particular 
case. This is my code:

connection = MySQLdb.connect (db = "netlogin", user="netadmin", pass = "r4nas", 
port="3308")

I appreciate if you give me assistance in the proper sintax of the last 
mentioned instruction.

Regards,



GatoLinux.
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to