package db_base; use strict; use DBI; use CGI::Carp qw( fatalsToBrowser ); sub new { my ( $class, $DSN, $user, $pass, $param ) = @_; $param = {} unless ref( $param ); my $self = { param => { %$param, dsn => $DSN, user => $user, pass => $pass, }, }; bless( $self, $class ); return $self; }#new sub connect_db { my $self = shift; unless ($self->{param}->{connect}) { $self->{dbh} = DBI->connect( $self->{param}->{dsn}, $self->{pa +ram}->{user}, $self->{param}->{pass} ) || croak("Cannot connect to database: $DBI::errstr\n$::bac +khtml"); $self->{param}->{connect} = 1; }#unless }#sub sub insert { my ( $self, $table, $columns, $data ) = @_; $self->{param}->{success} = 0; my $sql = "INSERT INTO `$table` (`" . join( "`,`", @$columns ) . ' +`) values(' . join( ",", map {'?'} @$columns ) . ')'; my $sth = $self->{dbh}->prepare($sql); $sth->execute(@$data) && {$self->{param}->{success} = 1} || croak( +"Cannot insert to $table: SQL = $sql\n $DBI::errstr\n$::backhtml"); }#sub
I get an error like:-use db_base; my $dbobj = new db_base( 'dsn', 'user', 'pass'); $dbobj->connect; $dbobj->insert( 'test_table', [ 'column' ], [ 'value' ]);
Cannot insert to test_test_table: SQL = INSERT INTO `test_test_table` +(`column`) values(?) Table 'affiliate.test_test_table' doesn't exist at c:/inetpub/wwwroot/cgi-bin/affiliate/db_base.pm line 52
In reply to Carp not working after change to objects by cosmicperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |