Richard Reina wrote:
> I have a perl->DBI->MySQL database app. that handles everything from my
> order entry to my accounts payable. I am in the process of cleaning up
> a lot of the code I've written to make it easier to maintain. I
> probably use the following DBI staement handle about fifty different
> places on different tables:
>
> use DBI;
> my $dbh =
> DBI->connect("DBI:mysql:database=operations;192.128.0.1","abuser","whisky");
> my $q = "INSERT into table (column, column, column, column) VALUES (?,
> ?, ?, ?)";
> my $sth = $dbh->prepare($q);
> $sth->execute($val1, $val2, $val3, $val4);
>
> Would it be best to create a class called INSERT that all scripts call
> when they need to insert create a new record in a table?
If you are using this in more than one script, I would make a module.
Package LocalModules::GroupName::Insert;
use DBI;
my @dbhs;
return( 1 );
END {
foreach ( @dbhs ){ $_->disconnect( );}
}
sub new {
my $proto = shift;
my $class = ref( $proto ) || $proto;
my $self = {};
my $self->{test_db} = @_ ? shift : 0;
};
bless( $self, $class );
return $self;
}
sub connect {
my $self = shift;
if( defined $self->{DBH} && ref( $self->{DBH} )){
return( $self->{DBH} );
}
my $dbi = join(':', 'dbi', 'mysql', ( $self->{test_db} ? 'test' : 'realdb'
)).';host=some.host';
$self->{DBH} = DBI->connect( $dbi, 'user', 'password' ) or die "Could not
connect to '$dbi';
push @dbhs, $self->{DBH};
$self->{DBH};
}
sub insert {
my $self = shift;
my $dbh = $self->{DBH} || $self->connect( );
unless( @_ == 4 ){
my( $p, $f, $l ) = caller( );
die ref( $self )."::insert( ) -- Need Four Parameters, called from $f line
$l\n";
}
my $sth = $self->{insert_STH} || ( $self->{insert_STH} = $dbh->prepare( $q ))
|| die;
return( $sth->execute( @_ ));
}
__END__
--Bill
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php