在DBD :: CSV中,f_ext属性中的/ r是什么意思?

时间:2010-05-08 07:26:00

标签: perl csv dbi

为什么只有第二个示例将扩展名附加到文件名,“。c / r”中的“/ r”是什么。

#!/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 个答案:

答案 0 :(得分:2)

强制要求扩展(而不是可选)。它来自DBD::FileDBD::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.

答案 1 :(得分:0)

我已经忘记了2.和3.论证的占位符;现在第一个例子也有效。

#!/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();