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

RE: DBI code problem!

From:
Seger, Jeffrey
Date:
February 2, 2001 06:42
Subject:
RE: DBI code problem!
Message ID:
834B92CB0E4CD21184A400A0C9D180BD0332AD91@NTEXC0001
I'm just guessing, but I'd say that you can't insert because 
1) you have called $dbh->disconnect() before doing your inserts and
2) you haven't opened the filehandle CSV that you are trying to read from

-----Original Message-----
From: TIBA, LIVIA [mailto:livia.tiba@bell.ca]
Sent: Friday, February 02, 2001 9:26 AM
To: dbi-users@perl.org
Subject: DBI code problem!


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