views:

69

answers:

2

Why does only the second example append the extension to the filename and what is the "/r" in ".csv/r" for.

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:f_dir=/home/mm", { RaiseError => 1, f_ext => ".csv/r"} );

my $table = 'new_1';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( id INT, name CHAR, city CHAR )" );

my $sth_new = $dbh->prepare( "INSERT INTO $table( id, name, city ) VALUES ( ?, ?, ?, )" );
$sth_new->execute( 1, 'Smith', 'Greenville' );
$dbh->disconnect();

# --------------------------------------------------------

$dbh = DBI->connect( "DBI:CSV:f_dir=/home/mm", { RaiseError => 1 } );
$dbh->{f_ext} = ".csv/r";

$table = 'new_2';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( id INT, name CHAR, city CHAR )" );

$sth_new = $dbh->prepare( "INSERT INTO $table( id, name, city ) VALUES ( ?, ?, ?, )" );
$sth_new->execute( 1, 'Smith', 'Greenville' );
$dbh->disconnect();
+2  A: 

It forces the extension to be required (rather than optional). It comes from DBD::File (a base class for DBD::CSV):

   f_ext
       This attribute is used for setting the file extension where (CSV)
       files are opened. There are several possibilities.

           DBI:CSV:f_dir=data;f_ext=.csv

       In this case, DBD::File will open only "table.csv" if both
       "table.csv" and "table" exist in the datadir. The table will still
       be named "table". If your datadir has files with extensions, and
       you do not pass this attribute, your table is named "table.csv",
       which is probably not what you wanted. The extension is always
       case-insensitive. The table names are not.

           DBI:CSV:f_dir=data;f_ext=.csv/r

       In this case the extension is required, and all filenames that do
       not match are ignored.
Chas. Owens
f_ext=.csvIn this case, DBD::File will open only "table.csv" if both "table.csv" and "table" exist in the datadir.f_ext=.csv/rIn this case the extension is required, and all filenames that do not match are ignored. Does this mean, that with "f_ext=.csv" I can read files with no extensions (in data). I tried to do so, but it didn't work.
sid_com
A: 

I've forgotten placeholders for the 2. and 3. argument; now the first example works too.

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:f_dir=/home/mm", undef, undef, { RaiseError => 1, f_ext => ".csv/r"} );

my $table = 'new_1';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( id INT, name CHAR, city CHAR )" );

my $sth_new = $dbh->prepare( "INSERT INTO $table( id, name, city ) VALUES ( ?, ?, ?, )" );
$sth_new->execute( 1, 'Smith', 'Greenville' );
$dbh->disconnect();

# --------------------------------------------------------

$dbh = DBI->connect( "DBI:CSV:f_dir=/home/mm", undef, undef, { RaiseError => 1 } );
$dbh->{f_ext} = ".csv/r";

$table = 'new_2';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( id INT, name CHAR, city CHAR )" );

$sth_new = $dbh->prepare( "INSERT INTO $table( id, name, city ) VALUES ( ?, ?, ?, )" );
$sth_new->execute( 1, 'Smith', 'Greenville' );
$dbh->disconnect();
sid_com