[Tutor] logging to a database

Kent Johnson kent37 at tds.net
Fri Nov 11 12:26:05 CET 2005


captnswing wrote:
> Hello all,
> I would like to log messages to a database (mysql)
> I found the example log_test14.py that comes with python logging  
> module http://www.red-dove.com/python_logging.html
> but that example is a bit greek for me ... :) and it doesnt work with  
> mysql

Have you used mysql from Python before? I looked at that example and it is not that complex if you understand how to program a database. I think what you have to do is

- install MySQL and create the database table that you want to log to. The SQL statement in the example shows the fields you might want, or you can use a subset of those fields if you don't need so much information.

- modify the example program a little:
change
  import mx.ODBC.Windows
to the correct import for the MySQL driver

change
  self.conn = mx.ODBC.Windows.connect(self.dsn, self.uid, self.pwd)
to the correct statement to connect to MySQL

change the SQL statement to correspond to your database schema


If you *haven't* used MySQL from Python before, you should probably learn a little about the basics first.

BTW the example is vulnerable to SQL injection attacks - if any of the log messages contain text from user input you should fix that. But get it working first, then come back and ask for help again.

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list