Hi Eko,
I did not test the script, but what looks strange to me is:
my ( $brg ) = @$row;
I doubt that this assignment really fills the variable list declared at the
beginning of the script and if it does it is at least highly "illegible".
I would suggest to use fetchrow_hashref, which returns all values in hash in
form fieldname => value.
my ($sql);
while (my $pData = $sth->fetchrow_hashref() ) {
my @fieldlist = ();
my @valuelist = ();
my @values = ();
foreach $key (sort keys %{$pData}) {
push (@fieldlist, $key);
push (@valuelist, '?');
push (@values, $pData->{$key};
}
my $sql = "INSERT INTO hipdf02 ";
$sql .= "(" . join (', ', @fieldlist) . ")";
$sql .= "VALUES (" . join (', ', @valuelist) . ")";
$dbh->do ($sql, @values);
};
If guess the first value you pass "undef" is for an id field, which will be
automatically added, if it is of type autoincrement.
Second point: Your SELECT statement only asks for column "brg", so how
should all the other fields be filled??
Shouldn't it be : SELECT * FROM hipdf02
Ehhm: You want data to be extracted from a dbf file to be inserted into a
mySql DB, but you only deal with ONE XBase DB. (hipdf02)
So where does your data to insert vom from???
Third point:
On normally uses PREPARE ...EXECUTE on statements where data
changes. (as in INSERT INTO db (<fieldlist> ) VALUES (?, ?,..? )
EXECUTE ($sth, [EMAIL PROTECTED]))
The SELECT statement you use should be executed directly without
PREPARE, but the INSERT could be transformed to use PREPARE.
The you would PREPARE the statement BEFORE the while loop and
just call $sth->execute ([EMAIL PROTECTED]);
Be warned: I had difficulties to use more than 12 variables/fields with this
method. Maybe the do (sql, @data) works better.
Regards,
Axel
> hi,
> Iwrite ascriptthat does extract data from a dbf file and insert the value
> into mysql.After I ran it, it
> does not insert the data into mysql.
>
> thescript:
>
> #!/usr/bin/perl -w
> use strict;
> use DBI;
> my ( $brg, $nama, $jenis, $stn, $hbeli, $maxi, $mini, $stn2, $isi, $hpp,
> $hbesar, $hkecil, $mvc,
> $lks, $sup, $merek, $part, $type, $nama2, $klink, $hp_std, $qprod, $qsales,
> $tgl_klr, $warna ) =
> "";
> my $dbh = DBI->connect("DBI:XBase:h:\/fuboru\/dataIMS" ) or die $DBI::errstr;
> my $sth = $dbh->prepare("select brg from hipdf02") or die $dbh->errstr();
> $sth->execute();
> while (my $row = $sth->fetchrow_arrayref() ) {
> #my ( $brg, $nama, $jenis, $stn, $hbeli, $maxi, $mini, $stn2, $isi, $hpp,
> $hbesar, $hkecil, $mvc,
> $lks, $sup, $merek, $part, $type, $nama2, $klink, $hp_std, $qprod, $qsales,
> $tgl_klr, $warna ) =
> @$row;
>
> my ( $brg ) = @$row;
>
> $dbh->do( "INSERT INTO hipdf02 (brg, nama, jenis, stn, hbeli, maxi, mini,
> stn2, isi, hpp, hbesar,
> hkecil, mvc, lks, sup, merek, part, type, nama2, klink, hp_std, qprod,
> qsales, tgl_klr, warna )
> VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
> ?, ? )",
> undef, $brg, $nama, $jenis, $stn, $hbeli, $maxi, $mini, $stn2, $isi, $hpp,
> $hbesar, $hkecil, $mvc,
> $lks, $sup, $merek, $part, $type, $nama2, $klink, $hp_std, $qprod, $qsales,
> $tgl_klr, $warna );
>
> print "$brg, $nama, $jenis, $stn, $hbeli, $maxi, $mini, $stn2, $isi, $hpp,
> $hbesar, $hkecil, $mvc,
> $lks, $sup, $merek, $part, $type, $nama2, $klink, $hp_std, $qprod, $qsales,
> $tgl_klr, $warna\t\t
> transferred\n";
> }
> $dbh->disconnect();
>
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs