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

发布于 2024-09-01 07:23:27 字数 1014 浏览 2 评论 0原文

为什么只有第二个示例将扩展名附加到文件名以及“.csv/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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2024-09-08 07:23:27

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

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.
汹涌人海 2024-09-08 07:23:27

我忘记了 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文