perl - 数据库 fetchrow_array 失败长截断 DBI 属性

标签 perl dbi

我正在使用 Perl 脚本从数据库中提取 URL,其中使用 fetchrow_array 从数据库中提取 URL,该脚本工作正常,直到我遇到一个很长的 URL georgelog24.blog.iskreni。 net/?bid=6744d9dcf85991ed2e4b8a258153a1ab&lid=ff9963b9a798ea335b75b5f7c0c295d1
然后它开始给我这个错误。

DBD::ODBC::st fetchrow_array failed: st_fetch/SQLFetch (long truncated DBI attribute LongTruncOk not set and/or LongReadLen too small) (SQL-HY000) [state was HY000 now 01004]
[Microsoft][ODBC SQL Server Driver]String data, right truncation (SQL-01004) at C:\test\multihashtest2.pl line 44.

我相信这是在数据库方面,因为我用来提取 URL 的代码之前已经工作过。我使用的数据库是MSSQL server 2005。

数据库中的URL列当前使用text类型,但我尝试将其更改为varchar(max)nvarchar(max) 但错误仍然存​​在。

经过一番尝试和错误后,我发现使用 fetchrow_array 成功查询的 url 最大长度为 81 个字符。由于 URL 有时可能会超出荒谬的长度,因此我无法对 URL 长度进行限制。

有人可以帮助我理解并建议解决这个问题吗?

仅供引用:第 44 行是下面代码中的第一行

while (($myid,$url) = $statement_handle->fetchrow_array()) { # executes as many threads as there are jobs to do 
    my $thread = threads->create(\&webcrawl); #initiate thread
    my $tid = $thread->tid;
    print "  - Thread $tid started\n";   #obtain thread no. and print
    push (@Threads, $thread);   #push thread into array for "housekeeping" later on
}

最佳答案

尝试:

#not anymore errors if content is truncated - you don't necessarily want this
$statement_handle->{'LongTruncOk'} = 1;

#nice, hard coded constant for the length of data to be read from Longs
$statement_handle->{'LongReadLen'} = 20000;
while (($myid,$url) = $statement_handle->fetchrow_array()) { # executes as many threads as there are jobs to do 
    my $thread = threads->create(\&webcrawl); #initiate thread
    my $tid = $thread->tid;
    print "  - Thread $tid started\n";   #obtain thread no. and print
    push (@Threads, $thread);   #push thread into array for "housekeeping" later on
}

另外,我建议您尝试Parallel::ForkManager用于并行化作业 - 我发现它比线程更直观且易于使用

关于perl - 数据库 fetchrow_array 失败长截断 DBI 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315397/

相关文章:

mysql - Perl DBI 一次执行多个 SQL 查询

mysql - DBI::DatabaseError:用户错误访问被拒绝当使用 Ruby DBI 连接远程 Mysql 服务器时

perl - 如何使用 ODBC 将 Perl 与 Excel 文件(*.xlsx 或 *.xls)连接起来?

arrays - 反转数组的哈希值

perl - 存储具有 PDL 作为属性的 Moose 对象

regex - PERL 正则表达式 - 返回不包含条件语句的结果

r - DB2 ODBC 连接在 R 4.2 上不起作用

perl - 如何在 Perl 中将跨多个时区的 unix 日期输出转换为 UTC?

perl - 当运行时间超过特定时间限制而不退出时,我可以告诉 perl 发出警报/信号吗?

r - 如何使用 R 列出来自 Postgres 数据库的表而不是 View ?