Despite what you might think, it's fairly easy to write MySQL-based Web applications with Zope. In this case study, find out how to do just that by building a Zope-based online poll which uses MySQL to store poll questions and user responses, and DTML Methods to extract and present this information.
Since I'm going to be using a MySQL database to store the poll's questions and answers, together with user responses, the first task is to design the relevant tables. Here's what I came up with:
#
# Table structure for table 'poll'
CREATE TABLE poll (
qid int(10) unsigned
NOT NULL auto_increment,
question varchar(255) NOT NULL,
response1 varchar(255)
NOT NULL,
response2 varchar(255) NOT NULL,
response3 varchar(255) NOT NULL,
votes1 int(10) unsigned DEFAULT '0' NOT NULL,
votes2 int(10) unsigned DEFAULT
'0' NOT NULL,
votes3 int(10) unsigned DEFAULT '0' NOT NULL,
date date DEFAULT
'0000-00-00' NOT NULL,
PRIMARY KEY (id)
);
#
# Column descriptions:
#
# qid
- a unique identifier for each poll question
# question - the poll question
#
response1 - possible response #1
# response2 - possible response #2
# response3
- possible response #3
# votes1 - number of votes for response #1
# votes2 - number
of votes for response #2
# votes3 - number of votes for response #3
# date - date
on which poll was posted
#
Just to get things started, I also INSERTed the first question into the database, together with three possible responses.
#
# Dumping data for table 'poll'#INSERT INTO poll (qid, question, response1, response2, response3,votes1, votes2, votes3, date) VALUES ( '1', 'The coolest comic strip ofall time is', 'Dilbert', 'Peanuts', 'Calvin and Hobbes', '0', '0', '0','2002-03-07');
At this point, it's also a good idea to verify that your Zope installation supports MySQL via the Z MySQL Database Adapter.