=encoding euc-jp =head1 NAME DBD::mysqlPP - Perlだけで記述したDBI用MySQLドライバ =head1 SYNOPSIS use DBI; $dsn = "dbi:mysqlPP:database=$database;host=$hostname"; $dbh = DBI->connect($dsn, $user, $password); $drh = DBI->install_driver("mysqlPP"); $sth = $dbh->prepare("SELECT * FROM foo WHERE bla"); $sth->execute; $numRows = $sth->rows; $numFields = $sth->{'NUM_OF_FIELDS'}; $sth->finish; =head1 EXAMPLE #!/usr/bin/perl use strict; use DBI; # データベースへ接続 my $dbh = DBI->connect("dbi:mysqlPP:database=test;host=localhost", "joe", "joe's password", {'RaiseError' => 1}); # テーブル'foo'をドロップ。'foo'が存在しない時は失敗するかもしれない。 # したがって evalで括る。 eval { $dbh->do("DROP TABLE foo") }; print "Dropping foo failed: $@\n" if $@; # 新たにテーブル'foo'を作成する。 これは失敗しないはずなので、 # エラーをキャッチしない。 $dbh->do("CREATE TABLE foo (id INTEGER, name VARCHAR(20))"); # 'foo'に任意のデータをインサートする。名前をクオートするために # $dbh->quote() を使用する。 # quoting the name. $dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")"); # 上記と同じ処理だがプレースホルダを使用する。 $dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen"); # テーブルからデータを読み出す。 my $sth = $dbh->prepare("SELECT id, name FROM foo"); $sth->execute(); while (my $ref = $sth->fetchrow_arrayref()) { print "Found a row: id = $ref->[0], name = $ref->[1]\n"; } $sth->finish(); # データベースから切断する。 $dbh->disconnect(); =head1 DESCRIPTION DBD::mysqlPPはPerlだけで実装したMySQLデーベース用のクライアントインタフェイスです。このモジュールはMySQLのサーバとクライアント間のネットワークプロトコルを実装しているので、libmysqlclientなどの他のMySQLクライアントライブラリを必要としません。つまりこれはMySQLが移植されていないOSからMySQLサーバへ接続できることを意味します。萌え〜ッ! 次の宣言でインタフェイスが使用可能になります。 use DBI; その後任意の数のMySQLデータベースサーバと接続し、簡単なオブジェクト指向のAPIを通じてそれらのデータベースに任意の数のクエリを送ることが出来ます。データベースハンドルとステートメントハンドルの2種類のオブジェクトが使用できます。次の記述で connectメソッドはデータベースハンドルを返します: $dbh = DBI->connect("dbi:mysqlPP:database=$db;host=$host", $user, $password, {RaiseError => 1}); データベースに接続できたなら、次の記述でSQLステートメントを実行することが出来ます: my $query = sprintf("INSERT INTO foo VALUES (%d, %s)", $number, $dbh->quote("name")); $dbh->do($query); quoteとdoメソッドの詳細は L を参照してください。別の方法は $dbh->do("INSERT INTO foo VALUES (?, ?)", undef, $number, $name); この場合quoteメソッドは自動的に実行されます。bind_paramメソッドについて L を参照してください。また、データベースハンドルの詳細については L を参照してください。 If you want to retrieve results, you need to create a so-called statement handle with: $sth = $dbh->prepare("SELECT id, name FROM $table"); $sth->execute(); This statement handle can be used for multiple things. First of all you can retreive a row of data: my $row = $sth->fetchow_arrayref(); If your table has columns ID and NAME, then $row will be array ref with index 0 and 1. See L below for more details on statement handles. I's more formal approach: =head2 Class Methods =over =item B use DBI; $dsn = "dbi:mysqlPP:$database"; $dsn = "dbi:mysqlPP:database=$database;host=$hostname"; $dsn = "dbi:mysqlPP:database=$database;host=$hostname;port=$port"; $dbh = DBI->connect($dsn, $user, $password); A C must always be specified. =over =item host The hostname, if not specified or specified as '', will default to an MySQL daemon running on the local machine on the default port for the INET socket. =item port Port where MySQL daemon listens to. default is 3306. =back =back =head2 MetaData Method =over 4 =item B @names = $dbh->tables; Returns a list of table and view names, possibly including a schema prefix. This list should include all tables that can be used in a "SELECT" statement without further qualification. =back =head2 Private MetaData Methods =over 4 =item ListDBs @dbs = $dbh->func('_ListDBs'); Returns a list of all databases managed by the MySQL daemon. =item ListTables B: This method is obsolete due to DBI's $dbh->tables(). @tables = $dbh->func('_ListTables'); Once connected to the desired database on the desired mysql daemon with the "DBI-"connect()> method, we may extract a list of the tables that have been created within that database. "ListTables" returns an array containing the names of all the tables present within the selected database. If no tables have been created, an empty list is returned. @tables = $dbh->func('_ListTables'); foreach $table (@tables) { print "Table: $table\n"; } =back =head1 DATABASE HANDLES The DBD::mysqlPP driver supports the following attributes of database handles (read only): $insertid = $dbh->{'mysqlpp_insertid'}; $insertid = $dbh->{'mysql_insertid'}; =head1 STATEMENT HANDLES The statement handles of DBD::mysqlPP support a number of attributes. You access these by using, for example, my $numFields = $sth->{'NUM_OF_FIELDS'}; =over =item mysqlpp_insertid/mysql_insertid MySQL has the ability to choose unique key values automatically. If this happened, the new ID will be stored in this attribute. An alternative way for accessing this attribute is via $dbh->{'mysqlpp_insertid'}. (Note we are using the $dbh in this case!) =item NUM_OF_FIELDS Number of fields returned by a I is performed, B surely returns I<0E0>. =back Cannot be used. =over 4 =item * 'mysql_use_result' attribute =item * 'ChopBlanks' attribute =item * 'is_blob' attribute =item * 'is_key' attribute =item * 'is_num' attribute =item * 'is_pri_key' attribute =item * 'is_not_null' attribute =item * 'length'/'max_length' attribute =item * 'NUUABLE' attribute =item * 'table' attribute =item * 'TYPE' attribute =item * 'mysql_type' attribute =item * 'mysql_type_name' attributei =back =head2 SQL Extensions Cannot be used. =over 4 =item * LISTFIELDS =item * LISTINDEX =back =head1 TODO Enables access to much metadata. =head1 SEE ALSO L, L =head1 AUTHORS Hiroyuki OYAMA Eoyama@crayfish.co.jpE =head1 COPYRIGHT AND LICENCE Copyright (C) 2002 Hiroyuki OYAMA. Japan. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.