This piece of code works:
#! /usr/bin/perl
use strict;
use warnings;
use DBI ':sql_types';
my $dbh = DBI->connect('DBI:ODBC:driver=Microsoft Access-Treiber (*.md
+b);dbq=db1.mdb',user,pass);
my $col = 'Testcol';
my $statement = "INSERT INTO table (`$col`) VALUES(?)";
my $coltype = get_type($col,$dbh);
$coltype = 'SQL_'.$coltype;
my $sth = $dbh->prepare($statement);
for(2..1000){
my $var = <STDIN>;
chomp $var;
no strict 'refs';
$sth->bind_param(1,$var,&{"DBI::$coltype"});
use strict 'refs';
$sth->execute($var) or die $dbh->errstr();
}
sub get_type{
my ($name,$dbh) = @_;
my ($sthcolinfo) = $dbh->column_info(undef,undef,undef,$name);
my $hashref = $sthcolinfo->fetchrow_hashref();
return $hashref->{TYPE_NAME};
}
But when I split the code in "script" and "module" it does not work.
I've tried this:
#! /usr/bin/perl
use strict;
use warnings;
use lib qw(.);
use DbiTest;
use DBI ':sql_types';
my $col = 'Testcol';
DbiTest->new($col);
module:
package DbiTest;
use strict;
use warnings;
use DBI ':sql_types';
sub new{
my ($class,$col) = @_;
my $dbh = DBI->connect('DBI:ODBC:driver=Microsoft Access-Treiber (*.
+mdb);dbq=db1.mdb',user,pass);
my $statement = "INSERT INTO table (`$col`) VALUES(?)";
my $coltype = get_type($col,$dbh);
$coltype = 'SQL_'.$coltype;
my $sth = $dbh->prepare($statement);
for(2..1000){
my $var = <STDIN>;
chomp $var;
no strict 'refs';
$sth->bind_param(1,$var,&{"DBI::$coltype"});
use strict 'refs';
$sth->execute($var) or die $dbh->errstr();
}
}
sub get_type{
my ($name,$dbh) = @_;
my ($sthcolinfo) = $dbh->column_info(undef,undef,undef,$name);
my $hashref = $sthcolinfo->fetchrow_hashref();
return $hashref->{TYPE_NAME};
}
1;
With that code the script dies with the following errormessage:
Usage: SQL_LONGVARCHAR() at DbiTest line 21
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.