programming-examples/perl/Database/Load text file to database.pl

16 lines
646 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
#!\usr\bin\perl
use warnings;
use strict;
use DBI;
my ($dbh, $sth, $firstname, $lastname, $destination, $rows);
$dbh=DBI->connect('dbi:mysql:test','root','password') || die "Error opening database: $DBI::errstr\n";
$sth=$dbh->prepare("INSERT INTO employee (firstname, lastname, destination)VALUES (? , ? , ? )");
$rows=0;
while (<>) {
chomp;
($firstname, $lastname, $destination) = split(/:/);
$sth->execute($firstname, $lastname, $destination)|| die "Couldn't insert record : $DBI::errstr";
$rows+=$sth->rows();
}
print "$rows new rows have been added to checkin";
$dbh->disconnect || die "Failed to disconnect\n";