Livia, see my comments below...

TIBA, LIVIA [[EMAIL PROTECTED]] wrote:
> 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();

I don't think you want to do this disconnect until after the
INSERT.  If you do this here, then you'll need to "connect"
again before doing the INSERT.

-- 
Hardy Merrill
Mission Critical Linux, Inc.
http://www.missioncriticallinux.com

> 
> $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

Reply via email to