ZopeMag's mascot the ZOPE fish


Article Finder
People
Issue 1 - Revision 5  /   June 14, 2002 


 
  ZopeMag Links:
Home Page
About the Fish
Issue 09
Issue 08
Issue 07
Issue 06
Issue 05
Issue 04
Issue 03
Issue 02
Issue 01
Latest Issue

 
 
Downloads
     
  Letter from the Editor:
   Welcome

Interviews:
In our first issue we interview the father of the Wiki and the inventor of Extreme Programming.

 Ward Cunningham

Tutorials:
Throughout the quarter we cover topics of interest to Zope developers, designers, and users.

  MetaFlow
  Databases and ZPTs
  Custom User Folders
  Zope and CSS
  Intranets

Product Review:
Every two weeks we review a new Zope Product

  Z Shrink
  External File
  DocumentLibrary
  ZWiki


 
 
Downloads
     
  Downloads:
Products we talk about in this issues Articles and Reviews

  ZShrink
  Zope Page Templates
  LocalFS
  SimpleZPTmyAdmin
  Extended User Folder
  MetaFlow
 
     



Databases and Zope Page Templates
Creating a Database User Interface with Zope Page Templates
- - - - - - - - - - - -

By Kristoph Kirchner | March 28, 2002



Creating New Tables

The original phpMyAdmin has a form which facilitates adding a new table. The user enters the name for the new table and how many fields it should have and then gets a form where he can enter the fields’ names, types, size, default values and so on. This is easy for the end user, but annoying for the programmer. At least it appears so to me. The problem here is that different field types have different syntaxes. For example, if I want to create a table with an integer field, I would do it like this:

CREATE TABLE int_table (my_int INTEGER)

But to create a table with a text field, I would have to state the size of the field in brackets right after the type:

CREATE TABLE text_table (my_text VARCHAR(50))

Because the field type and the field size are entered separately by the user, I needed to create the SQL statement depending on the field type. The following code, which is part of the createTable(self, REQUEST) method, shows how I create the CREATE TABLE statement. The variable no_length is a list containing the field types which do not need a size in brackets.

Partial Source Code of the createTable(self, REQUEST) Method

1. 1. the_query = 'CREATE TABLE ' + tab + '('
2. 2. for i in range(int(num_fields)):
3. if REQUEST.form['FieldType'+str(i)] not in no_length:
4. the_query = the_query + REQUEST.form['FieldName'+str(i)] + ' ' + REQUEST.form['FieldType'+str(i)] + '(' + REQUEST.form['FieldLength'+str(i)] + ') ' + REQUEST.form['FieldNull'+str(i)] + ','
5. if REQUEST.form['FieldType'+str(i)] in no_length:
6. the_query = the_query + REQUEST.form['FieldName'+str(i)] + ' ' + REQUEST.form['FieldType'+str(i)] + ' ' + REQUEST.form['FieldNull'+str(i)] + ','
7. the_query = the_query[:-1] + ')'


Here is an example for the variables:

FieldName0: ‘my_int_field’ FieldName1: ‘my_varchar_field’
FieldType0: ‘INTEGER’ FieldType1: ‘VARCHAR’
FieldLength0: ‘8’ FieldLength1: ‘50’
FieldNull0: ‘ NOT NULL’ FieldNull1: ‘’


The code above produces:

CREATE TABLE bla(my_int_field INTEGER NOT NULL, my_var VARCHAR(50) )

Executing SQL Queries:

Although it seems to be one of the more complex parts of SimpleZPTmyAdmin, writing the method which finds out the type of the SQL query a user entered in the query box and then executes this query was rather simple. This method, execSQLQuery(self, REQUEST), makes use of a method that comes with the DB class, the query(src, rdb) method. I only had to find out the first word of the query the user entered in the query box and then send the whole query to the query(src, rdb) method. src in this case is the query string. Please note: You have to set rdb to 0 (Zero) so that the method returns the result as a list and not as a string.

Final Word:

Although, I have only scratched the surface of the SimpleZPTmyAdmin project, I am sure – without too much trouble – you can figure out the parts I did not tell you about. :)

Because I started out trying to do a simple example of how to use ZPTs with databases, I only used External Methods to use Python code but it got bigger by the minute. I’ll convert the code into an actual Zope product in the coming months.

.  1  2  3
Tutorial End


Kristoph Kirchner: was born in Berlin in 1977 and finished school in 1996. After completing a degree in Commercial Correspondence, Kirchner went on to study Computer Sciences at the Technical University of Berlin. Since then Kirchner has been working for beehive writing e-books on Zope and documentation for projects of beehive's customers. Kirchner also co-authored the first German Zope book "Zope: Content Management and Web Application Server", the book "Zope: Web Application Construction Kit" and the book "Zope: How to Build and Deliver Web Applications".


shim
shim  ZopeMag is committed to bringing you the best in Zope Documentation. shim
shim


Home   Subscribe   FAQ   Contact   Write for us   Privacy Policy   Weekly News   PyZine   opensourcexperts.com  

Reproduction of material from any of ZopeMag's pages without prior written permission is strictly prohibited. Copyright 2003 - 2005 ZopeMag Zope/Plone hosting by Nidelven IT