Hi Gurus,

 

I am getting the following error while trying to insert into database.I am
getting the following only for this table, For all other tables I am able to
isnert data 

 

----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------

DBD::mysql::st execute failed: Column count doesn't match value count at row
1 a

t db_access.pm line 63.

 

 

Below is my script

 

my $XML_FILE="E:/Project/scripts/Master.xml";

my $ref = eval { XMLin($XML_FILE) };

my $DB_CLASS_OBJ;

my $RECS;

my $ATTEMPTED_RECS=0;

my $table="test";

my $LOGFILE="E:/project/database/logs/logs.txt";

(open my $FH_LOG_FILE, '>', $LOGFILE) or die "$!";

 

$DB_CLASS_OBJ=DB_ACCESS->new("fashion","127.0.0.1","3306","root","sqlroot123
");

$DB_CLASS_OBJ->DB_CONN;

$RECS=$DB_CLASS_OBJ->DB_VALIDATE($table);

 

print $FH_LOG_FILE "Before Inserting No. of Recs in $table are : $RECS\n";

 

foreach my $REC           (@{$ref->{Course}})

{

            $ATTEMPTED_RECS++;

            my $sql="insert into $table values
('2345','4355','834','894,'766');

            $DB_CLASS_OBJ->DB_INSERT("$sql");

            $DB_CLASS_OBJ->DB_FINISH;

            

}

 

$DB_CLASS_OBJ->DB_DISCONNECT;

 

close $FH_LOG_FILE;

 

 

Below is the module which I am using in the script..

#!/usr/bin/perl -w

 

package DB_ACCESS;

 

use strict;

use warnings;

use DBI;

 

sub new

{

 

            my $class=shift;

            my $db_name=shift;

            my $hostname=shift;

            my  $port=shift;

            my  $username=shift;

            my  $password=shift;

            my $DB_OBJ;

            my $ST_EXECUTE;

            bless
{db_name=>$db_name,hostname=>$hostname,port=>$port,username=>$username,passw
ord=>$password,DB_OBJ=>$DB_OBJ,ST_EXECUTE=>$ST_EXECUTE},$class;

}

 

 

sub DB_CONN

{

   my  $self=shift;

   my
$DSN="DBI:mysql:database=$self->{db_name};host=$self->{hostname};port=$self-
>{port}";

   $self->{DB_OBJ}=DBI->connect($DSN,$self->{username},$self->{password}) or
die "Cannot connet to database $DBI::errstr\n";

  

}

 

sub DB_DML

{

            my $self=shift;

            my $sql=shift;

    $self->{ST_EXECUTE}=$self->{DB_OBJ}->prepare($sql) or die "cannot
prepare SQL statement $DBI::errstr\n";

            $self->{ST_EXECUTE}->execute() or die "cannot execute the SQL
statement $DBI::errstr\n";

            my @row;

    while(@row=$self->{ST_EXECUTE}->fetchrow_array())

    {

             print"Row:@row\n";

     }

             warn "Error in fetching data from the table $DBI::errstr\n" if
$DBI::err;

}

 

sub DB_DDL

{

            my $self=shift;

            my $sql=shift;

    $self->{ST_EXECUTE}=$self->{DB_OBJ}->prepare($sql) or die "cannot
prepare SQL statement $DBI::errstr\n";

            $self->{ST_EXECUTE}->execute or die "cannot execute the SQL
statement $DBI::errstr\n";

            

}

sub DB_INSERT

{

            my $self=shift;

    my $query=shift;

            print "$query\n";

# To Turn OFF the Foriegn key checks

#          $self->{ST_EXECUTE}=$self->{DB_OBJ}->prepare("set
FOREIGN_KEY_CHECKS = 0");

#    $self->{ST_EXECUTE}->execute();

            $self->{ST_EXECUTE}=$self->{DB_OBJ}->prepare($query) or die
"cannot prepare SQL statement $DBI::errstr\n";

            $self->{ST_EXECUTE}->execute() or die "cannot execute the SQL
statement $DBI::errstr\n";

 

}

 

sub DB_FINISH

{

            my $self=shift;

            $self->{ST_EXECUTE}->finish;

}

sub DB_DISCONNECT

{

            my $self=shift;

 

            $self->{DB_OBJ}->disconnect or warn "Error in Disconnecting
$DBI::errstr\n";

}

 

sub DB_VALIDATE

{

 

            my $self=shift;

            my $table=shift;

    my $no_recs=$self->{DB_OBJ}->do("select * from $table");

            $self->{ST_EXECUTE}=$self->{DB_OBJ}->prepare("select * from
$table") or die "cannot prepare SQL statement $DBI::errstr\n";

            $self->{ST_EXECUTE}->execute or die "cannot execute the SQL
statement $DBI::errstr\n";

            my @row;

    

            while(@row=$self->{ST_EXECUTE}->fetchrow_array())

    {

             print"Row:@row\n";

 

     }

             warn "Error in fetching data from the table $DBI::errstr\n" if
$DBI::err;

    return $no_recs;

}

1;

 

----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------

 

 

I am passing the "insert into $table values
('2345','4355','834','894,'766');"  to DB_INSERT method in DB_ACCESS.pm  but
if  I print the sql_statement from the DB_INSERT method it is showing as 

 

insert into test values ('0','0','00001','00001') 

 

 

I am passing the correct number of values (which are equal to the number of
columns in the table)

 

 

Below is the table description:

 

 

| c_id                 | bigint(20) | NO   | PRI |         

| t_id                  | bigint(20) | NO   | PRI |         

| user_id             | bigint(20) | NO   | PRI |         

| profile_id           | bigint(20) | NO   | PRI |         |       |

| cl_id                 | bigint(20) | NO   | PRI |         |       |

 

 

5 rows in set (0.00 sec)

 

Can anybody let me know where I am going wrong.

 

 

Thanks,

PP

 

 

<<image001.gif>>

Reply via email to