in reply to Re: Re: Re: many to many join on text files
in thread many to many join on text files

....let's say it does support full outer join....is there a statement (that some rdbms have) to load delimited text files into tables without first creating the tables and specifying each column?
  • Comment on Re: Re: Re: Re: many to many join on text files

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: many to many join on text files
by tachyon (Chancellor) on Apr 15, 2004 at 01:41 UTC

    You have to create the table first. I don't think DBD::SQLite supports some syntax like LOAD DATA LOCAL INFILE 'blah.txt' INTO TABLE foo like MySQL and friends. All you need is something like (untested):

    use DBI; my $file = "C:/test.txt"; my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","",""); create the table to suit my $sql =<<SQL; CREATE TABLE stuff ( id INT, field CHAR(32), PRIMARY KEY(id) ) SQL $dbh->do( $sql ) or die "Can't create table\n" . $DBI::errstr; need as many ? bind values as fields my $sth = $dbh->prepare( "INSERT INTO stuff VALUES(?,?)"); open TXT, $file or die $!; while(<TXT>) { chomp; my ( $id, $field ) = split "\t"; # or whatever delim $sth->execute( $id, $field ); } close TXT; $sth->finish; $dbh->disconnect; print "Done!\n";

    cheers

    tachyon