有没有办法让这项工作? (将out_file写入我的主目录)
#!/usr/bin/perl
use warnings;
use strict;
use File::Spec::Functions;
use File::HomeDir;
use DBI;
my $database = 'my_db';
my $table = 'my_table';
my $user = 'user';
my $passwd = 'password';
my $dbh = DBI->connect( "DBI:mysql:$database;",
$user, $passwd, { RaiseError=>1, AutoCommit=>1 } );
$dbh->do( qq{ DROP TABLE IF EXISTS $table } );
$dbh->do( qq{ CREATE TABLE $table (
artikel INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
haendler CHAR(20) DEFAULT '' NOT NULL,
preis DOUBLE(16,2) DEFAULT '0.00' NOT NULL,
PRIMARY KEY( artikel, haendler ) ); } );
my $sth = $dbh->prepare( qq{ INSERT INTO $table
( artikel, haendler, preis )
VALUES( ?, ?, ? ) } );
$sth->execute( 1, 'Dotter', 1.35 );
$sth->execute( 2, 'Kahlo', 2.00 );
$sth->execute( 3, 'Schmidt', 4.30 );
$sth->execute( 3, 'Kahlo', 4.45 );
my $out_file = 'out_file.csv';
my $home_dir = File::HomeDir->my_documents;
$out_file = catfile $home_dir, $out_file;
$dbh->do( qq{ SELECT * INTO OUTFILE '$out_file'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"' LINES TERMINATED BY '\n'
FROM $table; } );
# DBD::mysql::db do failed: Can't create/write to file '/home/user/out_file.csv' (Errcode: 13) at ./mysql.pl line 44.
答案 0 :(得分:3)
您是否以具有写入该目录权限的用户身份运行脚本?
(对不起,我打算把它写成评论:/我失败了)
答案 1 :(得分:3)
#DBD :: mysql :: db do failed:无法在./mysql.pl第44行创建/写入文件'/home/user/out_file.csv'(错误代码:13)。
该错误消息完全是愚蠢的,MySQL的典型特征。此代码编号不是平台可移植的。 (POSIX仅指定符号名称,而不指定数字!)
未由业余爱好者编程的软件使用strerror
系统调用来提供描述性和可本地化的错误消息。
我不得不潜入/usr/include/asm-generic/errno-base.h
以查找号码;我在这里假设您也使用Linux:
#define EACCES 13 /* Permission denied */
所以你有它,dangerstat猜对了。