Scratchpad ToC
To keep my scratchpad from becoming a giant mess, here's a handy table of what can be found here:
SQLite FK PRAGMA problems
(Back to ToC)
DBD::SQLite FK example. Seems to fail in recent DBD::SQLite versions (but only when AutoCommit is off.)
use strict;
use warnings;
use DBI;
require Carp;
require v5.014_000;
my $dbh = DBI->connect(
"dbi:SQLite:db=:memory:",
"", "", {
AutoCommit => 0,
RaiseError => 1,
PrintError => 0,
}
);
defined $dbh or die "No dbh: failed database setup!";
printf( "SQLite DBD version: %s (%s)\n",
$dbh->{sqlite_version},
$DBD::SQLite::VERSION
);
# SQLite FK boilerplate setup follows.
# Here the PRAGMA is called to enable foreign_keys.
# However, PRAGMA will work on unknown/unsupported values.
# Thus we have to check that it actually got set if we care.
# With RaiseError, encapsulate DB work in eval:
eval {
# Attempt to enable FK support:
$dbh->do( 'PRAGMA foreign_keys(1)' );
# Now query the actual value:
my $fk_row = $dbh->selectrow_arrayref( 'PRAGMA foreign_keys' );
defined $fk_row or die "FK verify query failed. Should not happen.
+";
# And verify FK support is now enabled:
die "SQLite FK support is still disabled!" unless ($fk_row->[0] ==
+ 1);
# Indicate success if we got here, and disconnect to be nice.
print "Success: FK support was enabled as intended.\n";
$dbh->disconnect;
};
# Handle errors from DB work above: (safe as of Perl >=5.14.0.)
if ($@) {
my $err = $@;
# attempt rollback, avoids needless warnings:
eval { $dbh->rollback };
# report the error and a backtrace:
Carp::confess( "DB work error: $err" );
}
|