ãã¼ã¿ãã¼ã¹ã¨ããã°ã©ã ã®é£æº | Mojoliciouså ¥é
ããã¼ã¿ãã¼ã¹ã®åºç¤ãå¦ç¿ããå¾ã¯ãããã°ã©ã ãããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ããæ¹æ³ãå¦ã³ã¾ãããã
DBD::SQLiteã®ã¤ã³ã¹ãã¼ã«
ãã¾ãæåã«SQLiteã使ããããã«ããå¿ è¦ãããã¾ããWindowsã®å ´åã¯cpanã³ãã³ãã使ã£ã¦ãLinux, Mac OS Xã®å ´åã¯ãcpanmã³ãã³ãã使ã£ã¦DBD::SQLiteã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ãã¾ãã
# Windows cpan DBD::SQLite # Linux, Mac OS X cpanm DBD::SQLite
DBIx::Connectorã®ã¤ã³ã¹ãã¼ã«
ãPerlã«ã¯ãã¼ã¿ãã¼ã¹ãæä½ããå ´åã«DBIã使ãããã«ã¨è§£èª¬ããã¦ãããã¨ãå¤ãã§ãããWebãµã¤ããä½æããå ´åã¯ãDBIããã®ã¾ã¾ä½¿ã£ã¦ã¯ããã¾ãããWebãµã¤ããä½æããå ´åã¯ãããªãã©ã¼ã¯ãµã¼ãã¼ã§æ¬çªç°å¢ã«ããã¦é ç½®ããã®ã§ãããDBIã¯ãã¼ã¿ãã¼ã¹ã¨ã®æ¥ç¶ã®ç®¡çããã¦ããã¾ããã®ã§ãæ¥ç¶ãéåããã¨ããä¸å ·åãããã¾ãã
ãã§ããããDBIã«ãã¼ã¿ãã¼ã¹ã¨ã®æ¥ç¶ãèªåçã«ç®¡çãã¦ãããæ©è½ãã¤ããããã¢ã¸ã¥ã¼ã«ãå©ç¨ããå¿ è¦ãããã¾ããä»åã¯DBIã®æ©è½ã«ãæ¥ç¶ã®èªå管çæ©è½ãã¤ãããDBIx::Connectorã¨ããã¢ã¸ã¥ã¼ã«ã使ãã¾ãããã®ãããªã¢ã¸ã¥ã¼ã«ã¯ã³ãã¯ã·ã§ã³ããã¼ã¸ã£ã¼ã¨å¼ã°ããã®ã§è¦ãã¦ããã¾ãããã
ãDBIx::Connectorãã¤ã³ã¹ãã¼ã«ãã¾ãã
# Windows cpan DBIx::Connector # Linux, Mac OS X cpanm DBIx::Connector
ãã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶
ãã§ã¯DBIx::Connectorã使ã£ã¦ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ããããã¾ãæåã«DBIx::Connectorãªãã¸ã§ã¯ããä½æãã¾ããDBIx::Connectorãªãã¸ã§ã¯ããä½æããã«ã¯newã¡ã½ããã使ç¨ãã¾ãã
use DBIx::Connector; # æ¥ç¶ã®ä½æ my $dsn = 'dbi:SQLite:dbname=test.db'; my $conn = DBIx::Connector->new( $dsn, # ãã¼ã¿ã½ã¼ã¹å undef, # ã¦ã¼ã¶ã¼å undef, # ãã¹ã¯ã¼ã # DBIã®ãªãã·ã§ã³ { RaiseError => 1, PrintError => 0, AutoCommit => 1 } );
ãDBIx::Connectorã®newã®å¼æ°ã¯ãDBIã®connectã¡ã½ããã®å¼æ°ã¨åãã§ããnewã®ç¬¬1å¼æ°ã¯ãã¼ã¿ã½ã¼ã¹ã¨å¼ã°ãããã¼ã¿ãã¼ã¹ã«æ¥ç¶ããããã®æ å ±ãæå®ãã¾ãããã©ã¼ãããã¯å°ãç¹æ®ã§ããè¦ãã¦ãã¾ãã¾ãããã
ãã¼ã¿ã½ã¼ã¹ã®ä¸ã§æ¥ç¶ãããã¼ã¿ãã¼ã¹åãæå®ãããã¨ãã§ãã¾ããSQLiteã§ã¯ãã¼ã¿ãã¼ã¹ã¯åç´ã«ãã¡ã¤ã«ã¨ãã¦ä½æãããã®ã§ãã¼ã¿ãã¼ã¹åã¨ãã¦å¥½ããªååãæå®ãã¾ããæ»ãå¤ã«ã¯ãã¼ã¿ãã¼ã¹ãã³ãã«ã¨å¼ã°ãããã¼ã¿ãã¼ã¹ã¨éä¿¡ããããã®ãªãã¸ã§ã¯ããè¿ããã¾ãã
ããããå®è¡ããã¨ã«ã¬ã³ããã£ã¬ã¯ããªã«test.dbã¨ãããã¡ã¤ã«ãã§ãã¦ããã¯ãã§ãã
ã¦ã¼ã¶åã¨ãã¹ã¯ã¼ããæå®ãã¦æ¥ç¶ãã
ãSQLiteã§ä½æãããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ã«ã¯ã¦ã¼ã¶åããã¹ã¯ã¼ãã¯å¿ è¦ããã¾ãããã§ããMySQLãPostgreSQLãOracleãªã©ã®æ¨æºçãªãã¼ã¿ãã¼ã¹ã§ã¯ã¦ã¼ã¶åã¨ãã¹ã¯ã¼ããæ¥ç¶ã®ã¨ãã«æå®ããå¿ è¦ãããã¾ãã
ãDBIã使ç¨ãã¦ã¦ã¼ã¶åã¨ãã¹ã¯ã¼ããæå®ãã¦æ¥ç¶ããã«ã¯æ¬¡ã®ããã«ãã¾ãã第2å¼æ°ã«ã¦ã¼ã¶åã第3å¼æ°ã«ãã¹ã¯ã¼ããæå®ãã¾ãã
my $conn = DBIx::Connector->new($dsn, $user, $password);
ãªãã·ã§ã³ã®è§£èª¬
ãDBIx::Connectorã®newã¡ã½ããã®ç¬¬4å¼æ°ã«ã¯DBIã®ãªãã·ã§ã³ãæå®ãããã¨ãã§ãã¾ãã
DBIx::Connector->new($dsn, $user, $password, {opt1 => $val1, opt2 => $val2, ..});
ãDBIã®ããã©ã«ãã®åä½ã¯ã¨ã©ã¼ãçºçããå ´åã«è¦åãæ¨æºã¨ã©ã¼åºåã«åºåããããã«ãªã£ã¦ãã¾ãã
ãã§ããè¦åã§ã¯ãªããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºåãã¦ããã°ã©ã ãçµäºããã»ããããããããã«æãã¾ãã
ãè¦åãåºåããªãããã«ããã«ã¯æ¬¡ã®ããã«ãã¾ãã
PrintError => 0
ãã¾ãã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºåãã¦ããã°ã©ã ãçµäºããããã«ããã«ã¯æ¬¡ã®ããã«ãã¾ãã
RaiseError => 1
ãã¾ãDBIã«ã¯AutoCommitã¨å¼ã°ãããªãã·ã§ã³ãããã¾ããããã¯ã³ãããã¨å¼ã°ããå¦çãèªåçã«è¡ããè¡ããªããã決ãããªãã·ã§ã³ã§ãããã¼ã¿ãã¼ã¹ã®ãã©ã¤ãã«ãã£ã¦ããã©ã«ãã®å¤ãç°ãªãå¯è½æ§ãããã¾ãã®ã§æ示çã«æå®ãã¦ãããã»ããè¯ãã¨æãã¾ãããããã®å ´åã¯èªåçã«ã³ãããããããã«ãã¦ããã¦ãã©ã³ã¶ã¯ã·ã§ã³ãå¿ è¦ã«ãªã£ãå ´åã«AutoCommitã0ã«è¨å®ãã¾ãã
ãã§ãã®ã§ãã®ãããªãã¨ãè¸ã¾ãã¦ãã¼ã¿ãã¼ã¹ã«æ¥ç¶ããå ´åã¯æ¬¡ã®ãããªãµã³ãã«ã®ãããªè¨è¿°ã«ãã¦ããã®ãããã¨æãã¾ãã
ãã¼ã¿ãã¼ã¹ãã³ãã«ã®åå¾
ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ããããã«ã¯ãã³ãã¯ã·ã§ã³ããã¼ã¸ã£ã¼ãããã¼ã¿ãã¼ã¹ãã³ãã«ãåå¾ããå¿ è¦ãããã¾ãããã¼ã¿ãã¼ã¹ãã³ãã«ã¯dbhã¡ã½ããã§åå¾ã§ãã¾ãã
# ãã¼ã¿ãã¼ã¹ãã³ãã«ã®åå¾ my $dbh = $conn->dbh;
ãããã使ã£ã¦ããã¼ã¿ãã¼ã¹ã¸ã¢ã¯ã»ã¹ãè¡ãã¾ãã
SQLã®å®è¡
ãSQLã¨ã¯ãã¼ãã«ãä½æãããããã¼ã¿ãã¼ã¹ã«ãã¼ã¿ãç»é²ãããããã¼ã¿ãåãåºãããããããã«ä½¿ç¨ããåãåããè¨èªã®ãã¨ã§ããåºæ¬çãªSQLã«ã¤ãã¦ã¯ãããã解説ãã¾ããããã§ã¯ã¾ãSQLãå®è¡ããæ¹æ³ã解説ãã¾ãã
ãSQLãå®è¡ããæ¹æ³ã«ã¯2éãã®æ¹æ³ãããã¾ãã
- doã¡ã½ããã使ç¨ãã¦ç´æ¥å®è¡ããæ¹æ³
- prepareã¡ã½ããã§ã¹ãã¼ãã¡ã³ããã³ãã«ãæºåãã¦ããexecuteã¡ã½ããã§å®è¡ããæ¹æ³
ãäºã¤ã®æ¹æ³ãããã¾ãããååã¨ãã¦2çªç®ã®æ¹æ³ã使ãã¾ããWebãµã¤ãã§ã¯ã¦ã¼ã¶ã¼ããåãåã£ããã¼ã¿ããã¼ã¿ãã¼ã¹ã«æ¿å ¥ãããã¨ã«ãªãã¾ãããä¸çªç®ã®æ¹æ³ã¯ãã»ãã¥ãªãã£ã«ããå±éºãããã¾ãã®ã§ã2çªç®ã®æ¹æ³ã使ãããã«ãã¦ãã ããã
ãä»åã¯2çªç®ã®æ¹æ³ã ã解説ãã¾ãã
ã¬ã³ã¼ãã®è¿½å insert
ããã¼ãã«ã«ã¬ã³ã¼ããæ¿å ¥ããã«ã¯insertæã使ç¨ãã¾ãã
# ã¹ãã¼ãã¡ã³ããã³ãã«ã®æºå my $sth = $dbh->prepare('insert into book (id, title, author) values (?, ?, ?)'); # SQLã®å®è¡ my @params = ('00000001', 'Perl', 'kimoto'); $sth->execute(@params);
ãã¾ãæåã«prepareæã§ãã¹ãã¼ãã¡ã³ããã³ãã«ãæºåãã¾ãããã©ã¡ã¼ã¿ã¼ã®é¨åã¯ã?ããè¨è¿°ãã¾ããããã¯ãã¬ã¼ã¹ãã«ãã¼ã¨å¼ã°ãã¾ãã
ãSQLãå®éã«å®è¡ããã«ã¯ãã¹ãã¼ãã¡ã³ããã³ãã«ããexecuteã¡ã½ãããå¼ã³åºãã¾ããå¼æ°ã«ã¯ãã¬ã¼ã¹ãã«ãã¼ã«åãè¾¼ãå¤ãé åã§æå®ãã¾ãã
ãã¦ã¼ã¶ã¼ããåãåã£ãå¤ãå©ç¨ããã¨ãã¯ãå¿ ããã¬ã¼ã¹ãã«ãã¼ãå©ç¨ãã¦ãã ããããã¬ã¼ã¹ãã«ãã¼ã使ãã°ãå¤ã¯é©åã«ã¯ã©ã¼ãããã¾ãã®ã§ãå®å ¨ã§ãã
ã¬ã³ã¼ãã®æ´æ° update
ããã¼ãã«ã«ãããã¼ã¿ãæ´æ°ããã«ã¯updateæã使ç¨ãã¾ãã
# ã¹ãã¼ãã¡ã³ããã³ãã«ã®æºå my $sth = $dbh->prepare('update book set title = ? where author = ?'); # SQLã®å®è¡ my @params = ('Ruby', 'taro',); $sth->execute(@params);
ã¬ã³ã¼ãã®åé¤ delete
ãã¬ã³ã¼ããåé¤ããã«ã¯deleteæã使ç¨ãã¾ãã
# ã¹ãã¼ãã¡ã³ããã³ãã«ã®æºå my $sth = $dbh->prepare('delete from book where id = ?'); # SQLã®å®è¡ my @params = ('00000001'); $sth->execute(@params);
ã¬ã³ã¼ãã®åå¾ select
ãåãåå¾ããã«ã¯selectæã使ç¨ãã¾ããä»ã®SQLæã¨ç°ãªãã®ã¯ãåè¡ãåå¾ããããã«ãã§ããã¨ããå¦çãè¡ããã¨ã§ãã
# ã¹ãã¼ãã¡ã³ããã³ãã«ã®æºå my $sth = $dbh->prepare('select * from book where title like ?'); # SQLã®å®è¡ my @params = ('%Perl%'); $sth->execute(@params); # ãã§ãã while (my $row = $sth->fetchrow_hashref) { my $id = $row->{id}; my $title = $row->{title}; print "id: $id, title: $title\n"; }
ãfetchrow_hashrefã¡ã½ããã使ç¨ããã°ãåè¡ã®ãã¼ã¿ãããã·ã¥ãªãã¡ã¬ã³ã¹ã¨ãã¦åå¾ã§ãã¾ãããã¹ã¦ã®è¡ãåå¾ããã¾ã§ãwhileã«ã¼ãã®ä¸ã§ä½¿ç¨ãã¦ãã¹ã¦ã®è¡ãåå¾ããã¾ã§ç¹°ãè¿ãã¾ãã
æ¥æ¬èªã®æ±ã
ãããã°ã©ã ã®å é¨ã§ã¯å é¨æååãå©ç¨ãã¦ãå¤é¨ã¸åºåããã¨ãã¯ããã¤ãæååã«å¤æããã¨ãããã¨ãã解説ãã¾ãããããã¼ã¿ãã¼ã¹ã®å ´åãããã¨åãèãã§ãããã¼ã¿ãã¼ã¹ã«ãã¼ã¿ã渡ãã¨ãã¯ãå é¨æååããã¤ãæååã«å¤æããå¿ è¦ãããã¾ãã
ãããã©ãããã¯ãã¨ã¦ãããããããã§ãããã§ãããããèªåã§è¡ã£ã¦ãããsqlite_unicodeã¨ãããªãã·ã§ã³ãããã®ã§ããªãã·ã§ã³ããæå®ãã¦ããã°ãå¾ã¯ä½ãèããå¿ è¦ã¯ããã¾ããã
ãæ¥ç¶ããã¨ãã«ãªãã·ã§ã³ã¨ãã¦æå®ããããã«ãã¾ãããã
use DBIx::Connector; # æ¥ç¶ã®ä½æ my $dsn = 'dbi:SQLite:dbname=test.db'; my $conn = DBIx::Connector->new( $dsn, # ãã¼ã¿ã½ã¼ã¹å undef, # ã¦ã¼ã¶ã¼å undef, # ãã¹ã¯ã¼ã # DBIã®ãªãã·ã§ã³ { RaiseError => 1, PrintError => 0, AutoCommit => 1, sqlite_unicode => 1 # å é¨æååãèªåçã«UTF-8ãã¤ãæååã«å¤æ } );
ãµã³ãã«ã³ã¼ã
ãã§ã¯å®éã«åãããã¨ã®ã§ããããã°ã©ã ãæ¸ãã¦ã¿ã¾ããããSQLiteã§ã¯ããã¼ã¿ãã¼ã¹ãã¡ã¢ãªä¸ã«ä½æãããã¨ãã§ããã®ã§ã試é¨ãç°¡åã§ãããã¼ã¿ã½ã¼ã¹åã®ä¸ã§ããdbnameãã«ã:memory:ããæå®ãã¾ããã½ã¼ã¹ã³ã¼ãã¯UTF-8ã§ä¿åãã¦ãã ããã
use strict; use warnings; use utf8; use DBIx::Connector; use Encode 'encode'; # æ¥ç¶ã®ä½æ my $dsn = 'dbi:SQLite:dbname=:memory:'; my $conn = DBIx::Connector->new( $dsn, # ãã¼ã¿ã½ã¼ã¹å undef, # ã¦ã¼ã¶ã¼å undef, # ãã¹ã¯ã¼ã # DBIã®ãªãã·ã§ã³ { RaiseError => 1, PrintError => 0, AutoCommit => 1, sqlite_unicode => 1 } ); # ãã¼ã¿ãã¼ã¹ãã³ãã«ã®åå¾ my $dbh = $conn->dbh; # ãã¼ãã«ã®ä½æ my $create_table_sql = <<"EOS"; create table book ( id primary key, title not null default '', author not null default '' ); EOS $dbh->do($create_table_sql); # insertæ my $sth = $dbh->prepare('insert into book (id, title, author) values (?, ?, ?)'); $sth->execute('00000001', 'Perl', 'kimoto'); # updateæ $sth = $dbh->prepare('update book set title = ? where author = ?'); $sth->execute('Ruby', 'taro'); # deleteæ $sth = $dbh->prepare('delete from book where id = ?'); $sth->execute('00000001'); # insertæ $sth = $dbh->prepare('insert into book (id, title, author) values(?, ?, ?)'); $sth->execute('00000001', 'Perl Tutorial', 'æ¨æ¬'); $sth->execute('00000002', 'Perl advantage', 'å¥'); $sth->execute('00000003', 'Ruby Tutorial', 'æ´ä»'); # selectæ $sth = $dbh->prepare('select * from book where title like ?'); $sth->execute('%Perl%'); # ãã§ãã while (my $row = $sth->fetchrow_hashref) { my $id = $row->{id}; my $title = $row->{title}; my $author = $row->{author}; my $line = "id: $id, title: $title, author: $author\n"; # Windowsã®å ´åã¯cp932ã§ããã以å¤ã®OSã¯UTF-8ã§åºå my $enc = $^O eq 'MSWin32' ? 'cp932' : 'UTF-8'; print encode($enc, $line); }
ãåãtitleãã«ãPerlããå«ãã§ããå ´åã ããè¡ã®å 容ã表示ããã¾ãã
id: 00000001, title: Perl Tutorial, author: æ¨æ¬ id: 00000002, title: Perl advantage, author: å¥