Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: CSV to SQL?

by spm (Acolyte)
on Sep 15, 2001 at 15:04 UTC ( [id://112620] : note . print w/replies, xml ) Need Help??


in reply to CSV to SQL?

Ohh sorry... I knew I'd forgot something :) I want something like this:

INSERT INTO skjalaskra ('id', 'skjalamyndari', 'afhendingarar', 'skjalaflokkur', 'kassanumer', 'ork', 'fraar', 'tilar', 'innihald', 'yfirflokkur', 'athugasemdir') VALUES ('2', 'Félagsmálaráđuneytiđ', '7101', '1989', 'A', '2', '1', '1950', '1967', 'Bréfadagbók - sendendur', 'A - Bréfadagbćkur', '', '');

... (I think that's not the same line from the CSV, but it's the same context..)

Replies are listed 'Best First'.
Re: Re: CSV to SQL?
by demerphq (Chancellor) on Sep 15, 2001 at 17:42 UTC
    Ok. Here you go.

    BTW: CPAN is your friend...

    Oh yeah, I removed the ID column, for one you dont say where the id comes from and two if it was my table that column would be an identity column.

    use strict; use warnings; use Text::CSV_XS; my $csv=Text::CSV_XS->new({binary=>1}); while (<DATA>) { chomp; last unless $csv->parse($_); my @vals=$csv->fields; print "INSERT INTO skjalaskra ('skjalamyndari', ". "'afhendingarar', 'skjalaflokkur', 'kassanumer'," . "'ork', 'fraar', 'tilar', 'innihald', 'yfirflokkur',". " 'athugasemdir')\n". " VALUES ('".join("', '",@vals)."')\n"; }
    HTH.

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)