2010-05-08 41 views
1

爲什麼只有第二個示例將擴展名附加到文件名以及「.csv/r」中的「/ r」是什麼。在DBD :: CSV中,f_ext屬性中的/ 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

它強制擴展是必需的(而不是可選的)。它來自DBD::File(基類爲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. 
+0

f_ext =的.csv 在這種情況下,DBD ::文件將只開放 「table.csv」 如果 「table.csv」 和 「表」 中的datadir存在。 f_ext = .csv/r 在這種情況下,擴展名是必需的,所有不匹配的文件名都會被忽略。 這是否意味着,使用「f_ext = .csv」我可以讀取沒有擴展名(數據)的文件。我試圖這樣做,但沒有奏效。 – 2010-05-08 13:02:26

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(); 
相關問題