|
|
||||||||||||||||||
|
|
||||||||||||||||||
![]() |
![]() |
Issue 1 - Revision 5 / June 14, 2002
|
|||
|
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:
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:
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.
1 2 3Tutorial End
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ZopeMag is committed to bringing you the best in Zope Documentation. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
Reproduction of material from any of ZopeMag's pages without prior written permission is strictly prohibited. Copyright 2003 - 2005 ZopeMag |
|