1. 安裝DBI套件

wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.605.tar.gz

tar -xvf DBI-1.605.tar.gz

cd DBI-1.605

perl Makefile.PL
make
make test
make install

2. 安裝 Data::ShowTable

wget http://search.cpan.org/CPAN/authors/id/A/AK/AKSTE/Data-ShowTable-3.3.tar.gz

tar -xvf Data-ShowTable-3.3.tar.gz

perl Makefile.PL
make
make test
make install

3. 安裝 DBD::mysql

wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.007.tar.gz

tar -xvf DBD-mysql-4.007.tar.gz

perl Makefile.PL
make
make test
make install

EXAMPLE

  #!/usr/bin/perl

  use strict;
  use DBI();

  # Connect to the database.
  my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
                         "joe", "joe's password",
                         {'RaiseError' => 1});

  # Drop table 'foo'. This may fail, if 'foo' doesn't exist.
  # Thus we put an eval around it.
  eval { $dbh->do("DROP TABLE foo") };
  print "Dropping foo failed: $@\n" if $@;

  # Create a new table 'foo'. This must not fail, thus we don't
  # catch errors.
  $dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))");

  # INSERT some data into 'foo'. We are using $dbh->quote() for
  # quoting the name.
  $dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");

  # Same thing, but using placeholders
  $dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

  # Now retrieve data from the table.
  my $sth = $dbh->prepare("SELECT * FROM foo");
  $sth->execute();
  while (my $ref = $sth->fetchrow_hashref()) {
    print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
  }
  $sth->finish();

  # Disconnect from the database.
  $dbh->disconnect();



創作者介紹
創作者 sclin0323 的頭像
sclin0323

James Lin's weblog

sclin0323 發表在 痞客邦 留言(0) 人氣()