use strict; use Text::CSV_XS; use DBI; my $db = DBI->connect( ... ); $db->do( "drop table if exists new_table" ); my $csv = Text::CSV_XS->new(); open( my $infile, "filename.csv" ) or die "filename.csv: $!"; # get CSV header my $hdr = $csv->getline( $infile ); my $create = "create table new_table (" . join( " varchar(255),", @$hdr ) . " varchar(255))"; $db->do( $create ); # ... at this point, you could prepare an insert statement # for mysql, loop over $csv->getline() and execute the insert # for each row. # # but if you use LOAD DATA INFILE instead, it'll be much faster ...