use strict;
use warnings;

use DBI;

my $database = "books.db";
# my $database = ":memory:";
# メモリ上のデータベースへ接続
my $dbh = DBI->connect("dbi:SQLite:dbname=$database");

# テーブルの作成
my $create_table = "create table books (" .
                       "title," .
                       "author" .
                   ");";
$dbh->do($create_table);

# insert文の実行
$dbh->do("insert into books (title, author) values ('Perl', 'taro');");
$dbh->do("insert into books (title, author) values ('C++', 'taro');");
$dbh->do("insert into books (title, author) values ('Python', 'ken');");
$dbh->do("insert into books (title, author) values ('Java', 'mamoru');");

# update文の実行
$dbh->do("update books set title = 'Ruby' where author = 'mamoru'");

# delete文の実行
$dbh->do("delete from books where author = 'ken';");

# select文の実行
my $sth = $dbh->prepare("select * from books;");
$sth->execute;

# 各行のフェッチ
while (my @row = $sth->fetchrow_array) {
    # 各行を出力
    print join(', ', @row), "\n";
}

# データベースの切断
$dbh->disconnect;


