Using Python to Query MySQL over the Net

Mihai shows how Python can be used to create a CGI script to enable elegant searching.

Python is a wonderful language. The MySQL module makes it easy to create small programs to retrieve data from a MySQL database. Python is also great for CGI scripting. Thus, having a database available to study through the web browser is just several lines of code away.

The application I have shown here is rather limited. The user can only search for five variables and in a fixed way, predetermined by the HTML form. However, it is conceivable for the user to write his or her own database queries through a textarea input field and view the query results on-line. In fact, the possibilities are limited only by the programmer's imagination.


Mihai Bisca, (AKA 5dan), is a former winner of the Romanian Go Championship. He spends countless hours playing with his Slackware Linux and dreams of working over the Internet from home. In his other, daytime life, he works as an ophthalmologist.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Python CGI script -> MySQL database tables retrieving

Sampath Girish's picture

Hi Mihai Bisca,
This is sampath. I am trying to retrieve tables from database usng Python CGI scripts and MySQL as backend. I am able to display an empty web page and unable to get the tables from database.
Its not displaying a line 'Hi' which i kept after

import MySQLdb
import cgi, Cookie

print 'HI'

I ran this along with script for retrieving tables in database in IDLE. It ran successfully. But now its unable to display in web browser. How can i do that?

please send me the reply to my email id if possible. Its

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState