gpt4 book ai didi

mysql - DBD::mysql:如何将 "SELECT * INTO file"写入我的主目录?

转载 作者:行者123 更新时间:2023-11-29 04:14:06 25 4
gpt4 key购买 nike

有没有办法让它工作?(将 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.

最佳答案

# DBD::mysql::db do failed: Can't create/write to file '/home/user/out_file.csv' (Errcode: 13) at ./mysql.pl line 44.

那个错误消息完全是愚蠢的,MySQL 的典型。此代码编号不可平台移植。 (POSIX 只指定符号名称,不指定数字!)

非业余爱好者编写的软件使用 strerror 系统调用来提供描述性和可本地化的错误消息。

我不得不深入到 /usr/include/asm-generic/errno-base.h 以查找数字;我在这里假设您也使用 Linux:

#define EACCES 13/* 权限被拒绝 */

好了,dangerstat 猜对了。

关于mysql - DBD::mysql:如何将 "SELECT * INTO file"写入我的主目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2134390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com