2009年1月4日星期日

[PerlChina] perl读取access数据库时由于值里包含windows路径的"\"导致的问题

环境:

perl读取access数据库

使用DBI建立连接:

my $DSN = "driver=Microsoft Access Driver (*.mdb);dbq=d:\\jk\
\test.mdb";
my $dbh = DBI->connect("dbi:ODBCDSN",'','ytwk')
or die "Can't connect to Database: $DBI::errstr";

读取记录:

my $sth = $dbh->prepare( q{
SELECT * FROM testDB
}) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";

显示出前三列:

print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";

print "Field names: @{ $sth->{NAME} } \n";

my $ary_ref;

open (FILE1, ">a.txt";

while ( $ary_ref = $sth->fetchrow_arrayref) {

print FILE1 ("$ary_ref->[0] : $ary_ref->[1] : $ary_ref->[2] \n";
print FILE1 ("$ary_ref->[0] \n";
}


当运行的时候报告错误:

DBD::ODBC::st fetchrow_arrayref failed: [Microsoft][ODBC Microsoft
Access Driver
]字符串数据,右截断 在列号 3 (CopyDetail) (SQL-01004) at D:\jk\test1.pl line 31.

[Microsoft][ODBC Microsoft Access Driver]字符串数据,右截断 在列号 3 (CopyDetail
) (SQL-01004) at D:\jk\test1.pl line 39.


我检查后发现,是由于第三列内容包含windows路径的"\"导致的,当运行到 while ( $ary_ref = $sth-
>fetchrow_arrayref )的时候,就开始报告错误

请问各位,我该怎么做才能使perl正确读取"\"字符呢。

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina 论坛"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

没有评论: