File: params_in_error.pl

package info (click to toggle)
libdbd-odbc-perl 1.61-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,424 kB
  • sloc: perl: 8,928; ansic: 6,456; makefile: 33; sql: 8
file content (71 lines) | stat: -rw-r--r-- 1,981 bytes parent folder | download | duplicates (6)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# $Id$
#
# Code to demonstrate new (experimental) odbc_getdiag* and how you can find
# the bound parameter/column in error
#
use DBI qw(:sql_types);
use strict;
use warnings;
use Data::Dumper;
use DBD::ODBC qw(:diags);

my $h = DBI->connect('dbi:ODBC:baugi','sa','easysoft',
                     {RaiseError => 1, PrintError => 0});

eval {
    local $h->{PrintError} = 0;
    $h->do(q/drop table test/);
    $h->do(q/drop table test2/);
};

$h->do(q/create table test (a int, b int)/);
$h->do(q/create table test2 (a varchar(20), b varchar(20))/);

my $s = $h->prepare(q/insert into test values(?,?)/);
$s->bind_param(1, 'fred');
$s->bind_param(2, 1);
eval {
    $s->execute;
};
if ($@) {
    # NOTE from 1.34_3 calling odbc_getdiag* would clear DBI's errors
    # and so if you wanted them you'd have to call DBI's error methods first.
    # From 1.34_4 calling odbc_getdiag* will not clear DBI's errors.
    my @diags = $s->odbc_getdiagrec(1);
    my $dbierr = $s->errstr;
    print <<"EOT";
DBI error is $dbierr
which was created from the ODBC diagnostics:
  $diags[0]
  $diags[1]
  $diags[2]
EOT
    my $p = $s->odbc_getdiagfield(1, SQL_DIAG_COLUMN_NUMBER);
    print "The parameter in error is $p\n";
}

$h->do(q/insert into test2 values(?,?)/, undef, 1, 'fred');
$s = $h->prepare(q/select a,b from test2/);
$s->execute;
my ($a, $b);
$s->bind_col(1, \$a, {TYPE => SQL_INTEGER});
$s->bind_col(2, \$b, {TYPE => SQL_INTEGER});
eval {
    $s->fetch;
};
if ($@) {
    # NOTE from 1.34_3 calling odbc_getdiag* would clear DBI's errors
    # and so if you wanted them you'd have to call DBI's error methods first.
    # From 1.34_4 calling odbc_getdiag* will not clear DBI's errors.
    my @diags = $s->odbc_getdiagrec(1);
    my $dbierr = $s->errstr;
    print <<"EOT";
DBI error is $dbierr
which was created from the ODBC diagnostics:
  $diags[0]
  $diags[1]
  $diags[2]
EOT
    my $p = $s->odbc_getdiagfield(1, SQL_DIAG_COLUMN_NUMBER);
    print "The column in error is $p\n";
}