Load text file to database : MySql « Database « Perl






Load text file to database

   

#!\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";

   
    
    
  








Related examples in the same category

1.Deleting Entries
2.Dumping a Query's Results
3.Error Diagnostic Variables
4.Handling Quotes
5.Inserting into a Database
6.MySQL Data Types
7.Preparing a Statement Handle and Fetching Results
8.Pushing Query Results to an Array to Find Wildcard Hosts
9.Query parameter binding
10.Retrieving Query Results Listing MySQL Users and Hosts
11.Retrieving an Index ID