Hi,
I'm on XP Pro using PythonWin, but I use a remote virtual unix machine on a server somewhere to run scripts. Using Python 2.5, MySQL 5.0
The problem is that I have a need to regularly query several MySQL databases on different machines to do my analysis. So I decided to try and organise myself a bit better and have some class structure with common code in appropriate levels of it.
I have set the top class to default to the database I use most often. But I wanted the derived classes to overwrite the host, port and db. Nothing I try seems to work. Obviously I am too old and need to be taken outside and shot. Alternatively somebody could point out the error of my ways I would be eternaly grateful. PS I can't spell either.
So I have a top class MySQLconnection as follows.
-
import sys
-
import MySQLdb
-
-
class MySQLconnection:
-
def __init__ (self, username, password):
-
self.username=username
-
self.password=password
-
self.host_details=abc.def.ghi.com'
-
self.port_number=1234
-
self.db_name='lots_of+things'
-
self.sslparms = { 'something': '/dev/null' }
-
self.db = MySQLdb.connect(
-
host=self.host_details,
-
port=self.port_number,
-
user=self.username,
-
passwd=self.password,
-
db=self.db_name)
-
self.cursor = self.db.cursor()
-
-
def single_result(self):
-
row = self.cursor.fetchone()
-
if row is not None:
-
return row[0]
-
-
def array_result(self):
-
rows = self.cursor.fetchall()
-
if rows is not None:
-
return [row[0] for row in rows]
-
-
def result(self):
-
return self.cursor.fetchall()
-
and a dervided class for each Database as follows
-
mport sys
-
import MySQLdb
-
import MySQLconnection
-
-
class StatsConnection(MySQLconnection.MySQLconnection):
-
self.host_details='some other database'
-
self.port_number=1236
-
self.db_name='other_things'
-
self.sslparms = { 'qwerty': '/dev/null' }
-
-
def some database specifc methods etc etc
-
Then an example query becomes
-
import sys
-
import StatsConnection
-
-
username="TopSecret"
-
password="ExtremelyTopSecret"
-
-
stats=StatsConnection.StatsConnection(username, password)
-
-
def select_thing():
-
stats.cursor.execute("""select e.thing from
-
database.tbl_things as e limit 1""")
-
return stats.single_result()
-
-
x=select_rs()
-
print x
-
-
obviously the self is wrong in the derived class. What I am trying to do is have the derived class overwrite the specifics for the database connection it represnts.