I'd like to query my local MySQL db and send the results to a locally
served web page. I've poked around and found complex examples using
web2py and pylons.... What's best for beginners? Can somebody lay out
the basic steps for me?
thanks,
Greg
You need to use some sort of web server and some sort
of interface to a web server. Most web servers support
the CGI interface. Here is an example query using a
web server CGI interface and the WSGI/CGI translator.
This is one of many options, but it has the advantage
that you can move to a higher performance approach
later if you need to without changing code.
=== cut, beginning of file
#!/Library/Frameworks/Python.framework/Versions/Current/bin/python
import sys
import wsgiref.handlers
import MySQLdb
def application(env, start_response):
connection = MySQLdb.connect(host="localhost", user="me",
passwd="xyz", db="mydatabase")
cursor = connection.cursor()
cursor.execute("select * from MY_TABLE")
start_response("200 OK", [('Content-Type', 'text/html')])
for x in cursor.fetchall():
yield str(x)
yield "<br>\n"
# serve a CGI request using the application
wsgiref.handlers.CGIHandler().run(application)
=== end of file
Here is a link to the running example I just tested:
http://aaron.oirt.rutgers.edu/cgi-bin/mysql.cgi
The first thing you should try is getting the "hello world"
CGI script running before adding the MySQL query.
I had to make the CGI script executable and have it point
to my Python installation in the first line. Please see
http://docs.python.org/library/cgi.html
for a discussion of CGI scripting. I also had to mess
with some directory permissions because my MySQLdb on my
Mac in inside a Python Egg... to get around these sorts
of problems you may have to look at your server error log.
Also please see the WHIFF quickstart for a discussion
of setting up a WSGI/WHIFF based CGI script.
Good luck!
-- Aaron Watters
===
% man less
less is more.