English 中文(简体)
1. 更新输入文档一栏,从数据库中提取价值
原标题:update a column in input file by taking value from Database in perl
  • 时间:2011-01-07 15:49:05
  •  标签:
  • perl

投入文件:

1,a,USA,,
2,b,UK,,
3,c,USA,,

i 想从表一中提取数值来更新投入文件第4栏。

我的法典认为:

my $number_dbh = DBI->connect("DBI:Oracle:$INST", $USER, $PASS ) or die "Couldn t
connect to datbase $INST";
my $num_smh;
print "connected 
 ";
open FILE , "+>>$input_file" or die "can t open the input file";
print "echo 
";
while(my $line=<FILE>)
{
   my @line_a=split(/,/,$line);
   $num_smh = $number_dbh->prepare("SELECT phone_no from book where number = $line_a[0]");
   $num_smh->execute() or die "Couldn t execute stmt, error : $DBI::errstr";
   my $number = $num_smh->fetchrow_array();
   $line_a[3]=$number;
}
问题回答

就像你的数据一样,见CSV格式。 http://p3rl.org/Parse%3a%3aCSV”rel=“nofollow” Parse:CSV。

<代码>+>>> don t do so. 我想. 事实上,在测试过程中,似乎根本没有做任何事情。 此外,<代码>+< 非常奇怪:

 % cat file.txt
1,a,USA,,
2,b,UK,,
3,c,USA,,
 % cat update.pl
#!perl

use strict;
use warnings;

open my $fh,  +< ,  file.txt  or die "$!";
while ( my $line = <$fh> ) {
    $line .= "hello
";
    print $fh $line;
}
 % perl update.pl
 % cat file.txt
1,a,USA,,
1,a,USA,,
hello
,,
,,
hello
 %

<代码>+>

实际上,你想要做的是写新档案,然后复制旧档案。 开放一份同声读/像你一样进入一个伤害世界的档案。

除此以外,您应使用<条码>开放式(>)的三维表格(“超级”档案名称),并使用弹性文件处理器(这些代号不是全球的,如果它们超出了你的档案范围,则自动关闭。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签