develooper Front page | perl.dbi.users | Postings from February 2001

DBI code problem!

From:
TIBA, LIVIA
Date:
February 2, 2001 06:26
Subject:
DBI code problem!
Message ID:
3A7AC38C.A6EC4D3A@bell.ca
Hi,
Could someone help me with this code?!

##########################
# Read a CSV file with "," as the separator, as exported by
# MS Excel. 
##########################
require DBI;
my $dbh = DBI->connect("DBI:CSV:");
$dbh->{'csv_tables'}->{'employees'} = {
      'eol' => "\n",
      'sep_char' => ",",
      'quote_char' => undef,
      'escape_char' => undef,
      'file' => './employees',
      'col_names' => ["lname", "fname", "uid", "pein", "email", "phone",
"cellphone"]
       };
$dbh->do("DROP TABLE employees");
$sth = $dbh->prepare("CREATE TABLE employees (lname CHAR(10), fname
CHAR(10), 
                      uid CHAR(10), pein CHAR(10), email CHAR(35), phone
CHAR(15), 
                      cellphone CHAR(15))")
       or die "Cannot prepare: " . $dbh->errstr();
$sth->execute() or die "Cannot execute: " . $sth->errstr();
$sth->finish();
$dbh->disconnect();

$sth = $dbh->prepare("INSERT INTO employees
(lname,fname,uid,pein,email,phone,cellphone)        

VALUES (?,?,?,?,?,?,?)") || die $dbh->errstr;
while(<CSV>) {
   chop;
   my ($lname, $fname, $uid, $pein, $email, $phone, $cellphone) = split
/,/;
   $sth->execute( $lname, $fname, $uid, $pein, $email, $phone,
$cellphone ) || die 

$dbh->errstr;
}

$sth = $dbh->prepare("SELECT * FROM employees");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
print("Lname = ", $row->{'lname'},
      ", Fname = ", $row->{'fname'},
      ", Userid = ", $row->{'uid'},
      ", PEIN = ", $row->{'pein'},
      ", Email = ", $row->{'email'},
      ", OfficePhone = ", $row->{'phone'},
      ", Cellphone = ", $row->{'cellphone'},"\n");
}
$sth->finish();
----------------------------------

It creates table 'employees', but can't do insert in this table. What is
wrong?!
If this table is create and populate will be recognize by Oracle, SQL
as SQL table, or I have to use DBD::Oracle? 
Thanks for any help!

Livia




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About