如何使用 Perl 的 DBI 将日期值从 Oracle 转换为 Excel?

发布于 2024-07-08 08:03:55 字数 3136 浏览 6 评论 0原文

我在一个非常简单的 Perl 过程中遇到了麻烦。 我基本上是在查询 Oracle 数据库,并且想将其加载到 Excel 中。 我已经能够使用 DBIx::Dump 并且它有效。 但是,我需要能够使用各种 Excel 格式化工具。 我认为 Spreadsheet::WriteExcel 是输出到 Excel 的最佳模块,它允许我做了更多格式化。

下面是代码和我收到的错误。 我基本上查询 Oracle、获取数据、加载到数组中并尝试写入 Excel。 由于某种原因,它正在进行某种比较,但它不喜欢数据类型。 例如,日期为“2008 年 10 月 25 日”。 高级副总裁是“S01”。 似乎是在说它们不是数字。

错误

Argument "01-NOV-08" isn't numeric in numeric ge <>=> at C:/Perl/site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 3414.
Argument "01-NOV-08" isn't numeric in pack ge <>=> ge <>=> at C:/Perl/site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 2157.

代码:

#!/usr/bin/perl -w 

#Set the Perl Modules
use strict; 
use DBI;
use Spreadsheet::WriteExcel;


# Connect to the oracle database
my $dbh = DBI->connect( 'dbi:Oracle:xxxx',
                        'xxxx',
                        'xxxx',
                      ) || die "Database connection not made: $DBI::errstr";


#Set up Query
my $stmt = "select 
                                   week_end_date, SVP, RD,
                                    DM, store, wtd_smrr_gain,QTD_SMRR_GAIN,
                                   wtd_bor_gain,QTD_BOR_GAIN,
                                   wtd_cust_gain,QTD_CUST_GAIN,
                                   wtd_CARD_CLOSED_OCT25,QTD_AVG_CARD_CL
                    from 
                          bonus_4Q_store
                    order by
                          store"; 

#Prepare Query
my $sth = $dbh->prepare($stmt); 

#Execute Query
$sth->execute() or die $dbh->errstr; 

my( $week_end_date,$SVP,$RD,$DM,$store,
    $wtd_smrr_gain,$QTD_SMRR_GAIN,
    $wtd_bor_gain,$QTD_BOR_GAIN,
    $wtd_cust_gain,$QTD_CUST_GAIN,
    $wtd_CARD_CLOSED_OCT25,$QTD_AVG_CARD_CL);

#binds each column to a scalar reference
$sth->bind_columns(undef,\$week_end_date,\$SVP,\$RD,\$DM,\$store,
                   \$wtd_smrr_gain,\$QTD_SMRR_GAIN,
                   \$wtd_bor_gain,\$QTD_BOR_GAIN,
                   \$wtd_cust_gain,\$QTD_CUST_GAIN,
                   \$wtd_CARD_CLOSED_OCT25,\$QTD_AVG_CARD_CL,);

#create a new instance
my $Excelfile = "/Test_Report.xls"; 
my $excel = Spreadsheet::WriteExcel->new("$Excelfile"); 
my $worksheet = $excel->addworksheet("WOW_SHEET");

#Create array shell
my @data;

#Call data and Write to Excel  
while ( @data = $sth->fetchrow_array()){ 
    my $week_end_date = $data[0]; 
    my $SVP = $data[1]; 
    my $RD = $data[2]; 
    my $DM = $data[3]; 
    my $store = $data[1]; 
    my $wtd_smrr_gain = $data[2]; 
    my $QTD_SMRR_GAIN = $data[3];
    my $wtd_bor_gain = $data[4];
    my $QTD_BOR_GAIN = $data[5];
    my $wtd_cust_gain = $data[6];
    my $QTD_CUST_GAIN = $data[7];
    my $wtd_CARD_CLOSED_OCT25 = $data[8];
    my $QTD_AVG_CARD_CL = $data[9];
    my $row = 0; 
    my $col = 0; 
    foreach my $stmt (@data) 
    { 
        $worksheet->write($row++, @data); 
        last; 
    } 
} 

print "DONE \n"; 
$sth->finish(); 
$dbh->disconnect();

I am having trouble with a very simple Perl process. I am basically querying an Oracle database and I want to load it into Excel. I have been able to use DBIx::Dump and it works. However, I need to be able to use a variety of Excel formatting tools. And I think Spreadsheet::WriteExcel is the best module that outputs to Excel that allows me do more formatting.

Below is the code and the error I am getting. I basically query Oracle, fetch the data, load into an array and try to write to Excel. For some reason it is doing some kind of comparison and it does not like the data types. For example, the date is '25-OCT-08'. The SVP is 'S01'. It seems to be saying that they are not numeric.

Error:

Argument "01-NOV-08" isn't numeric in numeric ge <>=> at C:/Perl/site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 3414.
Argument "01-NOV-08" isn't numeric in pack ge <>=> ge <>=> at C:/Perl/site/lib/Spreadsheet/WriteExcel/Worksheet.pm line 2157.

Code:

#!/usr/bin/perl -w 

#Set the Perl Modules
use strict; 
use DBI;
use Spreadsheet::WriteExcel;


# Connect to the oracle database
my $dbh = DBI->connect( 'dbi:Oracle:xxxx',
                        'xxxx',
                        'xxxx',
                      ) || die "Database connection not made: $DBI::errstr";


#Set up Query
my $stmt = "select 
                                   week_end_date, SVP, RD,
                                    DM, store, wtd_smrr_gain,QTD_SMRR_GAIN,
                                   wtd_bor_gain,QTD_BOR_GAIN,
                                   wtd_cust_gain,QTD_CUST_GAIN,
                                   wtd_CARD_CLOSED_OCT25,QTD_AVG_CARD_CL
                    from 
                          bonus_4Q_store
                    order by
                          store"; 

#Prepare Query
my $sth = $dbh->prepare($stmt); 

#Execute Query
$sth->execute() or die $dbh->errstr; 

my( $week_end_date,$SVP,$RD,$DM,$store,
    $wtd_smrr_gain,$QTD_SMRR_GAIN,
    $wtd_bor_gain,$QTD_BOR_GAIN,
    $wtd_cust_gain,$QTD_CUST_GAIN,
    $wtd_CARD_CLOSED_OCT25,$QTD_AVG_CARD_CL);

#binds each column to a scalar reference
$sth->bind_columns(undef,\$week_end_date,\$SVP,\$RD,\$DM,\$store,
                   \$wtd_smrr_gain,\$QTD_SMRR_GAIN,
                   \$wtd_bor_gain,\$QTD_BOR_GAIN,
                   \$wtd_cust_gain,\$QTD_CUST_GAIN,
                   \$wtd_CARD_CLOSED_OCT25,\$QTD_AVG_CARD_CL,);

#create a new instance
my $Excelfile = "/Test_Report.xls"; 
my $excel = Spreadsheet::WriteExcel->new("$Excelfile"); 
my $worksheet = $excel->addworksheet("WOW_SHEET");

#Create array shell
my @data;

#Call data and Write to Excel  
while ( @data = $sth->fetchrow_array()){ 
    my $week_end_date = $data[0]; 
    my $SVP = $data[1]; 
    my $RD = $data[2]; 
    my $DM = $data[3]; 
    my $store = $data[1]; 
    my $wtd_smrr_gain = $data[2]; 
    my $QTD_SMRR_GAIN = $data[3];
    my $wtd_bor_gain = $data[4];
    my $QTD_BOR_GAIN = $data[5];
    my $wtd_cust_gain = $data[6];
    my $QTD_CUST_GAIN = $data[7];
    my $wtd_CARD_CLOSED_OCT25 = $data[8];
    my $QTD_AVG_CARD_CL = $data[9];
    my $row = 0; 
    my $col = 0; 
    foreach my $stmt (@data) 
    { 
        $worksheet->write($row++, @data); 
        last; 
    } 
} 

print "DONE \n"; 
$sth->finish(); 
$dbh->disconnect();

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

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

发布评论

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

评论(2

渔村楼浪 2024-07-15 08:03:55

问题就在这里:

foreach my $stmt (@data) 
{ 
    $worksheet->write($row++, @data); # !!
    last; 
} 

write() 的正确语法是:

write($row, $column, $token, $format)

您缺少 $column 参数,在本例中该参数可能为 0。

如果 $stmt 是一个数组引用,那么你可以一次性编写它,如下所示:

$worksheet->write($row++, 0, $stmt); 

The problem is here:

foreach my $stmt (@data) 
{ 
    $worksheet->write($row++, @data); # !!
    last; 
} 

The correct syntax for write() is:

write($row, $column, $token, $format)

You are missing the $column argument, which in this case is probably 0.

If $stmt is an array ref then you can write it in one go as follows:

$worksheet->write($row++, 0, $stmt); 
忆离笙 2024-07-15 08:03:55

我猜它是以字符串的形式出现的,当您尝试将其插入日期列时,它没有隐式转换。

尝试像这样选择日期,它会将其转换为可用于进行比较的字符。

to_char(date, 'YYYY/MM/DD HH24:MI:SS')

然后

to_date(date, 'YYYY/MM/DD HH24:MI:SS') 

将其转换回插入日期。 这通常是您需要在 SQL 中执行的操作。

我记得,perl 有一个 DBI 跟踪工具,可以更好地了解正在发生的情况。

I would guess that it is coming out as a string, and when you try to insert it into the date column, there is no implicit conversion for it.

Try selecting the date like this, and it will turn it into a char that you can use to do compares.

to_char(date, 'YYYY/MM/DD HH24:MI:SS')

then

to_date(date, 'YYYY/MM/DD HH24:MI:SS') 

to convert it back to a date on insert. That is generally what you need to do in SQL.

As I recall, perl has a trace facility for DBI that might giver a better picture as to what is going on.

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