-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv2sqlite.pl
executable file
·87 lines (67 loc) · 1.85 KB
/
csv2sqlite.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/perl
=head1 SYNPOSIS
Import a CSV file or a bunch of them into a new SQLite3 DB.
This is not particularly clever, just creates tables based on the header rows,
and treats everything as VARCHAR.
However, it can be handy if you have a lot of CSV files to analyse and compare.
=head1 AUTHOR
Luke Hudson <[email protected]>
=cut
use 5.10.0; # Enable newer perl features such as 'say'
use warnings;
use strict;
use Data::Dumper;
use Carp;
use DBI;
use Text::CSV;
if (@ARGV < 1) {
say "Usage: $0 file.db [ file.csv file.csv ...]";
say "Input CSV files default to *.csv if not supplied on commandline";
exit 1;
}
our $dbfile = shift @ARGV;
our $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","")
or croak $!;
our $csv = Text::CSV->new;
my @headers;
sub init_table {
my ($tableName, $headers) = @_ or croak "init_table called without headers";
$tableName =~ s/(.*)\.[^.]*$/$1/;
my $colDef = join(', ', map {"\"$_\" VARCHAR(255)"} @headers);
my $sql = qq{CREATE TABLE "$tableName" (${tableName}_id INTEGER PRIMARY KEY, $colDef)};
print "$sql\n";
$dbh->do($sql);
$dbh->commit();
my $params = '?,' x scalar @headers;
chop($params);
$colDef = '"' . join('", "', @headers) . '"';
$sql = qq{ INSERT INTO "$tableName" ($colDef) VALUES ($params) };
print "$sql\n";
return $dbh->prepare($sql);
}
my @files = @ARGV;
@files = <*.csv> unless @files;
FILE: for my $fname (@files) {
local $| = 1;# Flush
print "LOAD $fname\n";
open CSV, '<', $fname
or croak "$!";
my $sth;
@headers = ();
LINE: while(<CSV>) {
last LINE unless $csv->parse($_);
my @columns = $csv->fields();
unless (@headers) {
@headers = @columns;
print join(' | ', @headers) . "\n";
$sth = init_table($fname, \@headers);
next LINE;
}
croak "No statement!" unless $sth;
$sth->execute(@columns);
print '.';
}
close CSV;
$dbh->commit();
}
$dbh->disconnect();