生成两个表的有关程序
戻る
#!C:/perl/bin/perl
# show_money_percent.pl
use strict;
use DBI;
use CGI qw/:standard/;
use HTML::Template;
my ( %t, @rec, @loop1, @loop2 );
$t{template} = HTML::Template->new(filename => 'money_percent.htm');
$t{dsn} = "DBI:mysql:host=localhost;database=cookbook";
$t{dbh} = DBI->connect($t{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n";
$t{dbh}->do("SET NAMES utf8");
if(!$t{dbh}){
print "SQL read ERROR!\n";
exit;
}
# money table
$t{sth} = $t{dbh}->prepare("select * from money");
$t{sth}->execute;
@loop1 = ();
while (@rec = $t{sth}->fetchrow_array) {
my %row = (
id => $rec[0],
English => $rec[1],
Chinese => $rec[2]
);
push(@loop1, \%row);
}
$t{sth}->finish;
# percent table
$t{sth} = $t{dbh}->prepare("select * from percent");
$t{sth}->execute;
@loop2 = ();
while (@rec = $t{sth}->fetchrow_array) {
my %row = (
id => $rec[0],
Rate => $rec[1]
);
push(@loop2, \%row);
}
$t{sth}->finish;
$t{dbh}->disconnect;
$t{template}->param(LOOP1 => \@loop1);
$t{template}->param(LOOP2 => \@loop2);
print $t{template}->output;
1;
--------------------------------------------------------------
money_percent
money_percent
http://localhost/index.html
--------------------------------------------------------------
DROP TABLE IF EXISTS money;
CREATE TABLE money
(
id INT AUTO_INCREMENT,
English char(30),
Chinese char(30),
PRIMARY KEY (id)
);
INSERT INTO money (English,Chinese) VALUES("JPY","日元");
INSERT INTO money (English,Chinese) VALUES("USD","美元");
INSERT INTO money (English,Chinese) VALUES("EUR","欧元");
INSERT INTO money (English,Chinese) VALUES("GBP","英镑");
INSERT INTO money (English,Chinese) VALUES("RMB","人民币");
--------------------------------------------------------------
DROP TABLE IF EXISTS percent;
CREATE TABLE percent
(
id INT AUTO_INCREMENT,
Rate varchar(10),
PRIMARY KEY (id)
);
INSERT INTO percent (Rate) VALUES("1.05");
INSERT INTO percent (Rate) VALUES("1.10");
INSERT INTO percent (Rate) VALUES("1.20");
INSERT INTO percent (Rate) VALUES("1.30");
INSERT INTO percent (Rate) VALUES("1.50");
INSERT INTO percent (Rate) VALUES("150");
INSERT INTO percent (Rate) VALUES("250");
--------------------------------------------------------------
戻る