わいえむねっと

Contents
Categories
Calendar
2009/12
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Monthly Archives
~2000/01
Recent Entries
RSS1.0
Templates
Information
Processed: 0.017 sec
Chashed: -
2009/12/30 Wed
SQLiteのデータ型がフリーダム過ぎる。

use strict;
use warnings;
use DBI;

unlink 'foo.db';
my $dbh = DBI->connect('dbi:SQLite:dbname=foo.db');

$dbh->do('CREATE TABLE foo(foo, bar INTEGER, baz TEXT, qux NULL)');
$dbh->do('INSERT INTO foo VALUES(1,1,1,1)');
$dbh->do("INSERT INTO foo VALUES('2','2','2','2')");
$dbh->do("INSERT INTO foo VALUES('a','a','a','a')");

foreach my $col('foo','bar','baz','qux')
{
    foreach my $val("1","'1'","2","'2'","'a'")
    {
        my $sth = $dbh->prepare("SELECT * FROM foo WHERE $col=$val");
        $sth->execute;
        $sth->fetch or warn "$col=$val";

        $sth = $dbh->prepare("SELECT * FROM foo WHERE $col=?");
        $sth->execute(eval $val);
        $sth->fetch or warn "$col=? $val";
    }
}

> perl datatype.pl
foo=? 1 at datatype.pl line 23.
foo='1' at datatype.pl line 19.
foo=? '1' at datatype.pl line 23.
foo=2 at datatype.pl line 19.
qux=? 1 at datatype.pl line 23.
qux='1' at datatype.pl line 19.
qux=? '1' at datatype.pl line 23.
qux=2 at datatype.pl line 19.

 (型省略)INTEGERTEXTNULL
1->1(*1)○/×(*2)○/○○/○○/×
1->'1'×/×○/○○/○×/×
'2'->2×/○○/○○/○×/○
'2'->'2'○/○○/○○/○○/○
'a'->'a'○/○○/○○/○○/○

*1)更新時の指定->参照時の指定
*2)ベタ展開/プレースホルダ