📄 00000000.htm
字号:
# All of the above arrays were returned in the same order, so that $fields[0], <BR># $types[0], $not_null[0] and $length[0] all refer to the same field. <BR> <BR>foreach $field (0..$#fields) { <BR> print "<TR>\n"; <BR>print "<TD>$fields[$field]<TD>$types[$field]<TD>"; <BR> print $length[$field] if $types[$field] eq 'SQL_CHAR'; <BR> print "<TD>"; <BR> print 'Y' if ($not_null[$field]); <BR> print "</tr>\n"; <BR>} <BR> <BR>print <<END_OF_HTML; <BR></table> <BR><p> <BR><B>Data</b><br> <BR><OL> <BR>END_OF_HTML <BR> <BR># Now we step through the data, row by row, using DBI::fetchrow_array(). <BR># We save the data in an array that has the same order as the informational <BR># arrays (@fields, @types, etc.) we created earlier. <BR>while(my(@data)=$table_data->fetchrow_array) { <BR> print "<LI>\n<UL>"; <BR> for (0..$#data) { <BR> print "<LI>$fields[$_] => $data[$_]</li>\n"; <BR> } <BR> print "</ul></li>"; <BR>} <BR> <BR>print <<END_OF_HTML; <BR></ol> <BR></body></html> <BR> END_OF_HTML <BR>An Example DBI Application <BR>DBI allows for the full range of SQL queries supported by MySQL and mSQL. As an example, consider a database used by a school to keep track of student records, class schedules, test scores, and so on. The database would contain several tables, one for <BR>class information, one for student information, one containing a list of tests, and a table for each test. MySQL and mSQL's ability to access data across tables--such as the table-joining feature--enables all of these tables to be used together as a <BR>coherent whole to form a teacher's aide application. <BR> <BR>To begin with we are interested in creating tests for the various subjects. To do this we need a table that contains names and ID numbers for the tests. We also need a separate table for each test. This table will contain the scores for all of the <BR>students as well as a perfect score for comparison. The test table has the following structure: <BR> <BR>CREATE TABLE test ( <BR> id INT NOT NULL AUTO_INCREMENT, <BR> name CHAR(100), <BR> subject INT, <BR> num INT <BR>) <BR>The individual tests have table structures like this: <BR> <BR>CREATE TABLE t7 ( <BR> id INT NOT NULL, <BR> q1 INT, <BR> q2 INT, <BR> q3 INT, <BR> q4 INT, <BR> total INT <BR>) <BR>The table name is t followed by the test ID number from the test table. The user determines the number of questions when he or she creates the table. The total field is the sum of all of the questions. <BR> <BR>The program that accesses and manipulates the test information is test.cgi. This program, which follows, allows only for adding new tests. Viewing tests and changing tests is not implemented but is left as an exercise. Using the other scripts in this <BR>chapter as a reference, completing this script should be only a moderate challenge. As it stands, this script effectively demonstrates the capabilities of DBI:[1] <BR> <BR>#!/usr/bin/perl -w <BR> <BR>use strict; <BR>require my_end; <BR> <BR>use CGI qw(:standard); <BR>my $output = new CGI; <BR>use_named_parameters(1); <BR> <BR># Use the DBI module. <BR>use DBI; <BR># DBI::connect() uses the format 'DBI:driver:database', in our case we are <BR># using the MySQL driver and accessing the 'teach' database. <BR>my $dbh = DBI->connect('DBI:mysql:teach'); <BR>The add action itself is broken up into three separate functions. The first <BR>function, add, prints out the template form for the user to create a new <BR>test. <BR>sub add { <BR> $subject = param('subject') if (param('subjects')); <BR> $subject = "" if $subject eq 'all'; <BR> <BR> print header, start_html('title'=>'Create a New Test', <BR> 'BGCOLOR'=>'white'); <BR> print <<END_OF_HTML; <BR><H1>Create a New Test</h1> <BR><FORM ACTION="test.cgi" METHOD=POST> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="add2"> <BR>Subject: <BR>END_OF_HTML <BR> my @ids = (); <BR> my %subjects = (); <BR> my $out2 = $dbh->prepare("select id,name from subject order by name"); <BR> $out2->execute; <BR> # DBI::fetchrow_array() is exactly analogous to Msql::fetchrow() <BR> while(my($id,$subject)=$out2->fetchrow_array) { <BR> push(@ids,$id); <BR> $subjects{"$id"} = $subject; <BR> } <BR> print popup_menu('name'=>'subjects', <BR> 'values'=>[@ids], <BR> 'default'=>$subject, <BR> 'labels'=>\%subjects); <BR> print <<END_OF_HTML; <BR><br> <BR>Number of Questions: <INPUT NAME="num" SIZE=5><br> <BR>A name other identifier (such as a date) for the test: <BR> <INPUT NAME="name" SIZE=20> <BR><p> <BR><INPUT TYPE=SUBMIT VALUE=" Next Page "> <BR> <INPUT TYPE=RESET> <BR></form></body></html> <BR>END_OF_HTML <BR>} <BR> <BR> <BR>-- <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 162.105.17.153] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -