Using Python to Query MySQL over the Net
The user should be able to search the database for several items like the tournament name, the black and white player names, the date of the game (at least the year) and also the winner of the game. All this information is available in the SGF files along with the actual game record.
So, I wrote the HTML document shown in Listing 1. Yes, I like to write HTML by hand, and there is no need for anything more complicated. Of course, “myhost” has to be replaced with the actual hostname of the web server. The reader will also notice the HTML form acts by calling the CGI script named search.py once the submit button is clicked. The appearance of the document loaded by Netscape is shown in Figure 3.
Each item in the HTML form has to be described in the database. To this end, I created a new database to store the tables. This is done as root, by typing:
myhost:~# mysqladmin create igo
where igo is the new database name. This is not enough though, because only root will have access to the new database. To grant only SELECT privileges to all users for the new database, root has to type:
myhost:~# mysql mysql
mysql> insert into db values (
'%','igo','','Y','N', 'N', 'N', 'N','N','N', 'N',
'N', 'N');
This changes the table database in the MySQL internal database by
adding an entry for the igo database. Then, the MySQL dæmon
has to be restarted or the command mysql> flush
privileges; has to be typed so that the MySQL dæmon
becomes aware of the privilege change.
Now all users should be able to access data in database igo, but only root can change the data.
Creating the tables (one for each tournament) was easy. For instance, a table named gosei, for the tournament Gosei, was made with the following commands:
myhost:~# mysql igo
mysql> create table gosei (
black varchar(30),
white varchar(30),
dt date,
rez varchar(30),
fname varchar(30),
;
Query OK, 0 rows affected, (0.00 sec)
mysql>
The table has five columns: black player's name, white player's name, the date of the game, the result and finally, the corresponding SGF file name.
Loading the data in the table is another matter. I suppose one way would be to type:
mysql> insert into gosei values ('Cho Chikun', 'Kato Masao', '1987-07-03', 'B+3.5', 'gosei87_1.sgf' );
but I'd rather learn a new programming language than enter that a thousand times. Fortunately, there is another way of loading data in a MySQL table—from a text file. Each row in the file matches a row in the table, and the fields are separated by white spaces, as shown in Table 1.
Table 1. Text to Be Loaded to MySQL Table
Suppose this file is also named gosei. To pass the data to a MySQL table, one would write
mysql> load data infile "gosei" into table gosei;
Then a query result should look like Table 2.
Table 2. Loading from a Text File
The reader might ask “Okay, but isn't making a huge text file another burden?” Actually, that's an easy task for yet another small Python script (that I will not show, because it's not directly related to our topic).
With the igo database created and the tables loaded with data, there was only one thing left to be done: write the Python CGI script that would take input from the user via the HTML form, query the database and produce a list of matching game files.
This program, named search.py is presented in Listing 2 [at the LJ ftp site]. It makes use of two great modules imported in the third and fourth lines. The CGI module is almost like magic: it just gets the data submitted by the HTML form as a Python dictionary. The programmer does not need to be concerned with details like the method (GET or POST) used to send form data to the CGI script. Don't you love Python?
The MySQL module is also easy to use. With four simple statements, it opens the connection to the desired database, sends the query, gets the results and stores them in a list of row lists (a Python list whose members each contain one row).
The script has the following structure: getting the HTML form data, creating the database query string according to the form data and querying a table and printing the matching results.
Since there is one for each Go tournament, step three is repeated through a for loop as many times as necessary. The Python code and the appended comments are rather self-explanatory, so I will only comment on the lines:
print '<li><a href="http://myhost' + \ '/cgi-bin/getsgf.py?file=' + e[0] + '">' print e[0] + '</a>'
Here, e[0] is an SGF file name. Instead of merely printing the filename (which would be of limited help to the user), the print statements create an HTML anchor that sends the filename to the CGI script getsgf.py. This last script (which for the sake of simplicity will not be shown here) searches for the actual path to the SGF file and starts a Java applet to display the contents of the file in a nice, graphical way.
Of course, both search.py and getsgf.py must be made executable and moved to the cgi-bin directory. Also, a final trap to avoid: all the SGF game files must be placed somewhere within the DocumentRoot directory (as defined in httpd configuration files) in order to be found by a CGI script.
The result of running search.py is displayed in Figure 4 as seen by the user. Clicking on each filename starts the applet shown in Figure 5, which gradually displays the moves of the game.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Reply to comment | Linux Journal
1 hour 8 min ago - Reply to comment | Linux Journal
8 hours 2 min ago - Reply to comment | Linux Journal
8 hours 18 min ago - Favorite (and easily brute-forced) pw's
10 hours 9 min ago - Have you tried Boxen? It's a
16 hours 1 min ago - seo services in india
20 hours 33 min ago - For KDE install kio-mtp
20 hours 33 min ago - Evernote is much more...
22 hours 34 min ago - Reply to comment | Linux Journal
1 day 7 hours ago - Dynamic DNS
1 day 7 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?







Comments
Python CGI script -> MySQL database tables retrieving
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 girishmsampath@gmail.com