1. #!/bin/perl

  2. # load module
  3. use DBI;

  4. # connect
  5. my $dbh = DBI->connect("DBI:mysql:database=db2;host=localhost", "joe", "guessme", {'RaiseError' => 1});

  6. # execute INSERT query
  7. my $rows = $dbh->do("INSERT INTO users (id, username, country) VALUES (4, 'jay', 'CZ')");
  8. print "$rows row(s) affected ";

  9. # execute SELECT query
  10. my $sth = $dbh->prepare("SELECT username, country FROM users");
  11. $sth->execute();

  12. # iterate through resultset
  13. # print values
  14. while(my $ref = $sth->fetchrow_hashref()) {
  15.     print "User: $ref-> ";
  16.     print "Country: $ref-> ";
  17.     print "---------- ";
  18. }

  19. # clean up
  20. $dbh->disconnect();
09-15 04:52
查看更多