MySQL Chapter Six (The MySQL perl API)

Documentation Version: 0.95

MySQL Version: 3.20.29

The MySQL Perl Adaptor

A Simple Perl interface to the MySQL database system

SUMMARY:

        
  use Mysql;
        
  $dbh = Mysql->Connect;
  $dbh = Mysql->Connect($host);
  $dbh = Mysql->Connect($host,$database);
  $dbh = Mysql->Connect($host,$database,$password);
  $dbh = Mysql->Connect($host,$database,$password,$user);
        
  $dbh->SelectDB($database);
        
  $sth = $dbh->ListFields($table);
     @arr = @{$sth->name};       
     @arr = @{$sth->length};     
     $value = $sth->numfields;  
     @arr = @{$sth->type};     
     @arr = @{$sth->is_num};  
     @arr = @{$sth->is_blob};
     @arr = @{$sth->is_not_null};
                                

  $sth = $dbh->Query($sql_statement);
        
  @arr = $dbh->ListDBs;
  @arr = $dbh->ListTables;
        
  @arr = $sth->FetchRow;
  %hash = FetchHash  $sth;
        
  $sth->DataSeek($row_number);

  $scalar = $dbh->sock;
  $scalar = $dbh->host;
  $scalar = $dbh->database;
  $scalar = $dbh->quote($binary_string);

This package is designed to be as close as possible to its C API counterpart.

Internally you are dealing with the two classes Mysql and Mysql::Statement. You will never see the latter, as you reach it through a statement handle returned by a Query or a ListFields statement. The only class you name explicitly is Mysql. It offers you the Connect command.

Because of limitations in how Perl deals with numeric values you will have problems using numbers larger than the size of a signed LONG. (2147483647). This can occur when using MySQL's unsigned LONG (DOUBLE) or LONGLONG (BIGINT) data types. Perl stores returned values as strings, but will automatically convert them to numbers when you use the values in a numeric context. This will cause them to be "truncated" to 2147483647 since Perl uses a signed LONG value to store such numbers.

You can use the following work arounds. First of all, always treat values that may be large as strings. As long as you do this they can be displayed and re-inserted into a database without incident. The same goes for inserting new values into tables. If you set a variable called $tmpvar to be equal to "4147483647" and then INSERT it into your database you should be fine. Note that the ""'s are important here, since they force Perl to treat the value as a string.

If you need to do calculations involving large numbers you should do them via the initial SELECT.


Connect

$dbh = Mysql->Connect;
$dbh = Mysql->Connect($host);
$dbh = Mysql->Connect($host,$database);
$dbh = Mysql->Connect($host,$database,$password);
$dbh = Mysql->Connect($host,$database,$password,$user);

This connects you with the desired host/database. With no argument or with an empty string as the first argument it connects to the UNIX socket /dev/mysql, which is a big performance gain. A database name in the second argument selects the chosen database within the connection. The return value is a database handle if the Connect succeeds, otherwise the return value is undef. If you are using a mysqld compiled with MIT threads you will be unable to use sockets. In this case specify your systems hostname for the host variable.

You may also optionally provide a username and password. If no user name is provided then the current login will be used. If no password is provided the connection will fail if the user has a password.

You will need this handle to gain further access to the database. You may issue multiple Connect statements, but be sure to use different variable names ($dbh1,$dbh2, $dbh3, etc.)

You can find out what socket, host and database a given handle is associated with by using doing the following.

$scalar = $dbh->sock;
$scalar = $dbh->host;
$scalar = $dbh->database;

The database will be undef if you specified only a host during the Connect.

Change Port

If you wish to change the port that MysqlPerl connects on, do the following.

$ENV{"MYSQL_TCP_PORT"}=3334;
$ENV{"MYSQL_UNIX_PORT"}="/tmp/mysql_new.sock";
use Mysql;
$dbh = Mysql->Connect($host);


SelectDB


ListFields


Query


ListDBs


ListTables