execute()) would be okay.
First some code:
use DBI;
$dbh = DBI->connect(
"dbi:mysql:database=$database",
$dbuser, $dbpass
);
print "Drop it\n";
$dbh->do('
DROP TABLE IF EXISTS `foobar`;
');
print "Create it\n";
$dbh->do(q(
CREATE TABLE `foobar` (
`id` int AUTO_INCREMENT,
`one` int DEFAULT 17,
`two` varchar(31) DEFAULT 'number',
PRIMARY KEY (`id`)
);
));
print "Insert row 1 via do(), NULL DEFAULT DEFAULT\n";
$dbh->do('
INSERT INTO `foobar` VALUES (NULL, DEFAULT, DEFAULT);
');
$sth = $dbh->prepare('
INSERT INTO `foobar` VALUES (?, ?, ?);
');
print "Insert row 2 via prepared statement, undef undef undef\n";
$sth->execute( undef, undef, undef );
I'd like to be able to use the column defaults in a Mysql table via Perl
DBI. Ideally, I'd have some flag that makes binding "undef" work as
DEFAULT, but an explicit bind value (either in ->bind_param() or
execute()) would be okay.
I see _nothing_ about using column defaults in my (c)2000 _Programming
the Perl DBI_ by Alligator Descartes[*] and Tim Bunce, but okay, that's
an old book. But I also see nothing about it in 'perldoc DBI' or
'perldoc DBD:mysql' either. Nor does a quick grep of the module turn
up ideas.
Am I out of luck, or am I going about this wrong?
First some code:
use DBI;
print "Insert row 1 via do(), NULL DEFAULT DEFAULT\n";
$dbh->do('
INSERT INTO `foobar` VALUES (NULL, DEFAULT, DEFAULT);
');
$sth = $dbh->prepare('
INSERT INTO `foobar` VALUES (?, ?, ?);
');
print "Insert row 2 via prepared statement, undef undef undef\n";
$sth->execute( undef, undef, undef );
print "Insert row 3 via prepared statement, undef 34 DEFAULT\n";
$sth->execute( undef, 34, DEFAULT );
print "Insert row 4 via prepared statement, undef DEFAULT 'Fault'\n";
$sth->execute( undef, DEFAULT, 'Fault' );
Insert row 1 via do(), NULL DEFAULT DEFAULT
Insert row 2 via prepared statement, undef undef undef
Insert row 3 via prepared statement, undef 34 DEFAULT
Insert row 4 via prepared statement, undef DEFAULT 'Fault'
DBD::mysql::st execute failed: Incorrect integer value: 'DEFAULT'
for column 'one' at row 1 at test-default line 41.
What have we got? SELECT *
Row: 1 17 number
Row: 2
Row: 3 34 DEFAULT
I'd like to be able to use the column defaults in a Mysql table via Perl
DBI. Ideally, I'd have some flag that makes binding "undef" work as
DEFAULT, but an explicit bind value (either in ->bind_param() or
execute()) would be okay.
with <eli$2209101827@qaz.wtf> Eli the Bearded wrote:
* Then you can go like this instead:
$dbh->prepare('INSERT INTO foobar VALUES (?, ?, DEFAULT)');
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 168:33:10 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,545 |