$sth->execute(@cols) ;In which @cols is the array with a value for each placeholder.
$cols[0] = undef ; # gives default value of that column $cols[1] = 'NULL' ; # interpreted as string.......
CREATE TABLE t1 ( int x default 100, int y ); INSERT INTO t1 (y) VALUES (1); SELECT * FROM t1;You get (100, 1) back. If you did INSERT INTO t1 VALUES (NULL, 1), you'll get NULL for x's value.
mysql> show create table t1 ; +-------+--------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------+ | t1 | CREATE TABLE `t1` ( `a` varchar(100) default 'TEST TABLE', `b` int(11) default '10' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+-------------------------------------------+This is my test script ph.pl:
#! /usr/bin/perl
use DBI;
use strict ;
my $dsn = "DBI:mysql:database=test;host=localhost";
my $dbh = DBI->connect($dsn, "root", "");
my $sth = $dbh->prepare("INSERT INTO t1 (a,b) VALUES(?,?)") ;
my @a = (undef,undef ) ;
$sth->execute(@a) ;
$dbh->disconnect() ;
Running this script gives:mysql> select * from t1 ; +------+------+ | a | b | +------+------+ | | NULL | +------+------+ 1 row in set (0.00 sec)So there is no NULL for column a!
mysql> desc t1 ; +-------+--------------+------+-----+------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+------------+-------+ | a | varchar(100) | YES | | TEST TABLE | | | b | int(11) | YES | | 10 | | +-------+--------------+------+-----+------------+-------+so they can be NULL!
Update: the MySQL docs say you can also do INSERT INTO t1 VALUES (DEFAULT, 20) to force the default value for a particular column. I'm not sure how you do that via DBI.
$cols[0] = undef ; # gives default value of that columnwhich I have shown to be untrue. Doing that WILL set it to NULL.
mysql> select * from t1 ; +------+------+ | a | b | +------+------+ | | NULL | +------+------+ 1 row in set (0.00 sec) mysql> select * from t1 where a is null ; +------+------+ | a | b | +------+------+ | | NULL | +------+------+ 1 row in set (0.00 sec)So, the value of column 'a' is not an empty string, but its 'NULL'!
perlmonks.org content © perlmonks.org and bart, cwry, davidrw, japhy, jeanluca
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03