Created
November 22, 2010 19:04
-
-
Save Tordek/710449 to your computer and use it in GitHub Desktop.
CSV to SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
# Read the first parameter: tablename. | |
my $tablename = shift; | |
# The first line holds the names of the fields. | |
my $fields = <>; | |
chomp $fields; | |
print "INSERT INTO $tablename ($fields) values"; | |
my $firstline = 1; | |
while (<>) { | |
chomp; | |
if ($firstline) { | |
# The first insert is special-cased to avoid a trailing comma. | |
print "\n"; | |
$firstline = 0; | |
} else { | |
# The rest of the VALUE groups are comma-separated. | |
print ",\n"; | |
} | |
print "('" . join ("','", split(/\s*,\s*/)) . "')"; | |
} | |
print ";\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment