Как да се свържа с MySQL чрез Perl

от MySQL UG Bulgaria

Jump to: navigation, search
  #!/usr/bin/perl  -w
  use DBI;
  use strict;
  # Connection parameters
  my $user= "root";
  my $password= "password";
  my $host= "localhost";
  # Establish the connection which returns a DB handle
  my $dbh= DBI->connect("dbi:mysql:database=mysql;host=$host",$user,$password)
  or die $DBI::errstr;
  # Prepare the SQL statement
  my $sth= $dbh->prepare("SELECT VERSION()")
  or die $DBI::errstr;
  # Send the statement to the server
  $sth->execute();my $numRows = $sth->rows;
  print "Rows returned: $numRows\n";
  my @row;
  while ( @row = $sth->fetchrow_array )
  {
    print "@row\n";
  }
  # Изпълнява заявката веднага без да е пускан $dbh->prepare и връща броя на добавените или променени редове
  # Не може да се използва за SELECT защото няма къде да върне резултат
  $dbh->do("INSERT INTO table values ('value1', 'value2')");
  # Close the connection
  $dbh->disconnect or die $DBI::errstr;
Лични инструменти