PDOテスト

以下の環境で、PDO (PHP Data Object) を実際に動かしてみた。

  • Ubuntu 8.04 on VMware Player
  • php5 5.2.4-2ubuntu5.1
  • php5-sqlite 5.2.4-2ubuntu5.1
  • sqlite3 3.4.2-2

参考資料は以下の通り。
http://japan.internet.com/developer/20071023/26.html

今回は、とりあえずPDOを介してSQLite3のデータベースが参照できることを確認するために、最小限のテストプログラムだけ動かした。

データベースの作成

以下のSQLファイルを作成して、テーブルを定義する。

  • db/create-table.sql:
create table items (
  id integer not null primary key,
  name text not null,
  due integer not null,
  complete integer null,
  priority integer not null,
  status integer not null
);
  • 実行結果
~/work/php-sqlite/db> sqlite3 todo.db3 < create-table.sql
~/work/php-sqlite/db> sqlite3 todo.db3
SQLite version 3.4.2
Enter ".help" for instructions
sqlite> .schema items
CREATE TABLE items (
  id integer not null primary key,
  name text not null,
  due integer not null,
  complete integer null,
  priority integer not null,
  status integer not null
);
sqlite>

次に、以下のSQLファイルを作成して、ダミーデータをDBに登録する。

  • db/dummy-data.sql
insert into "items"
  values(1, 'Buy anniversary present', 1184437800, null, 4, 1);
insert into "items"
  values(2, 'Send invoices', 1180549800, null, 5, 1);
insert into "items"
  values(3, 'Finish homework', 1180549800,  1180290600, 1, 0);
insert into "items"
  values(4, 'Visit Jane', 1180290600, null, 2, 1);
insert into "items"
  values(5, 'Finalize hotel reservations', 1181737000, null, 5, 1);
insert into "items"
  values(6, "Angie's birthday", 1182537000, null, 4, 1);
insert into "items"
  values(7, 'Weed the lawn', 1182237000, null, 1, 1);
  • 実行結果
~/work/php-sqlite/db> sqlite3 todo.db3 < dummy-data.sql
~/work/php-sqlite/db> sqlite3 todo.db3
SQLite version 3.4.2
Enter ".help" for instructions
sqlite> select * from items;
1|Buy anniversary present|1184437800||4|1
2|Send invoices|1180549800||5|1
3|Finish homework|1180549800|1180290600|1|0
4|Visit Jane|1180290600||2|1
5|Finalize hotel reservations|1181737000||5|1
6|Angie's birthday|1182537000||4|1
7|Weed the lawn|1182237000||1|1

テストプログラムの実行

リンク先のサンプルプログラムを参考にして、以下のテストプログラムを作成した。

<?php
try {
  $dbh = new PDO('sqlite:../db/todo.db3');

  $sth = $dbh->query("select * from items order by id");
  $items = $sth->fetchAll(PDO::FETCH_ASSOC);
  $sth = null;
  unset($dbh);
} catch (PDOException $e) {
  die('Error: ' . $e->getMessage());
}

foreach ($items as $row) {
  $line = array();
  foreach ($row as $key => $value) {
    array_push($line, "$key: $value");
  }
  print join($line, ", ") . "\n";
}
?>

このプログラムを実行するためには、pdo_sqlite.soがインストールされている必要がある。Ubuntu 8.04 では、php5-sqliteパッケージにこのライブラリが含まれていた。確認していないが、このほかに、sqlite.soあるいはsqlite3.soが必要かもしれない。

  • 実行結果
~/work/php-sqlite/php> php test1.php
id: 1, name: Buy anniversary present, due: 1184437800, complete: , priority: 4, status: 1
id: 2, name: Send invoices, due: 1180549800, complete: , priority: 5, status: 1
id: 3, name: Finish homework, due: 1180549800, complete: 1180290600, priority: 1, status: 0
id: 4, name: Visit Jane, due: 1180290600, complete: , priority: 2, status: 1
id: 5, name: Finalize hotel reservations, due: 1181737000, complete: , priority: 5, status: 1
id: 6, name: Angie's birthday, due: 1182537000, complete: , priority: 4, status: 1
id: 7, name: Weed the lawn, due: 1182237000, complete: , priority: 1, status: 1