redirecting stdout to mysql table

skip at pobox.com skip at pobox.com
Tue Nov 18 14:10:45 EST 2008


    n00b>     def openDb(self):
    n00b>         try:
    n00b>             self.db = MySQLdb.connect(host = self.db_host,
    n00b>                                         user = self.db_uname,
    n00b>                                         passwd = self.db_passwd,
    n00b>                                         db = self.db_name,
    n00b>                                         )
    n00b>
    n00b>             self.cur = self.db.cursor()
    n00b>             return self.db, self.cur
    n00b>         except Exception, e:
    n00b>             sys.stdout = sys.__stdout__
    n00b>             sys.stderr = sys.__stderr__
    n00b>             print e[0], e[1]
    n00b>             sys.exit(1)
    ...
    n00b>     def write(self, string):
    n00b>         s = string.strip('\n')
    n00b>         if not s=='':
    n00b>             self.openDb()
    n00b>             self.cur.execute(self.sql, (s))
    n00b>             self.db.commit()
    n00b>             self.closeDb()

You don't really want to open a new database connection for every write call
do you?

-- 
Skip Montanaro - skip at pobox.com - http://smontanaro.dyndns.org/



More information about the Python-list mailing list