Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Problem with code

by SamueD2 (Novice)
on Jan 31, 2003 at 13:50 UTC ( [id://231595]=perlquestion: print w/replies, xml ) Need Help??

SamueD2 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote this script and I keep getting errors. Can somebody tell me what I am doing wrong. Thanks in advance for your help.
use strict; use DBI; #use Win32::ODBC; use DBD::ODBC; if (-e "/tf_out.txt") { } else { die "File does not exist\n"; } #my $filename = shift || die "Please provide a filename.\n"; #my $Data = new Win32::ODBC("DSN=INSTDev;UID=sa;PWD=henry;"); #my $dbh = new Win32::ODBC("DSN=INSTDev;UID=sa;PWD=henry;"), my $dbh = DBI->connect( "dbi:ODBC:INSTDev", "sa", "henry", {RaiseError => 1, PrintError => 1, AutoCommit => 1} ) or die "Unable to connect: " . $DBI::errstr . "\n"; # Open your file open( INFILE, "/tf_out.txt" ) or die "Couldn't open file for reading.$ +!\n"; while( $line = <INFILE> ) { my @cols = split( "\t", $line ); $CLIENT = $data[0]; $RECORD = $data[1]; $CONTROL = $data[2]; $CUSIP = $data[3]; $FROMACCT = $data[4]; $F_IND = $data[5]; $TOACCT = $data[6]; $T_IND = $data[7]; $FP = $data[8]; $ORIGQTY = $data[9]; $DIV = $data[10]; $LTCG = $data[11]; $STCG = $data[12]; $SOCSEC = $data[13]; $BKRNO = $data[14]; $THIRDPARTYCD = $data[15]; $USERID = $data[16]; $EDATE = $data[17]; $ORIGAMT = $data[18]; $CLOSEDIND = $data[19]; $ADPSECNO = $data[20]; $BRNCH = $data[21]; $ACNT = $data[22]; $TYPE = $data[23]; $CHKDIGIT = $data[24]; $PDATE = $data[25]; $ACTLQTY = $data[26]; $ACTLAMT = $data[27]; $CERTIND = $data[28]; $REQST = $data[29]; $RDATE = $data[30]; $COMNTS = $data[31]; $TRNFTYPE = $data[32]; $FILLER1 = $data[33]; $ADR_L1 = $data[34]; $ADR_L2 = $data[35]; $ADR_L3 = $data[36]; $ADR_L4 = $data[37]; $ADR_L5 = $data[38]; $ADR_L6 = $data[39]; $FILLER_B = $data[40]; $CERTNO1 = $data[41]; $CERTSHRS1 = $data[42]; $CERTNO2 = $data[43]; $CERTSHRS2 = $data[44]; $CERTNO3 = $data[45]; $CERTSHRS3 = $data[46]; $CERTNO4 = $data[47]; $CERTSHRS4 = $data[48]; $CERTNO5 = $data[49]; $CERTSHRS5 = $data[50]; $CERTNO6 = $data[51]; $CERTSHRS6 = $data[52]; $CERTNO7 = $data[53]; $CERTSHRS7 = $data[54]; $CERTNO8 = $data[55]; $CERTSHRS8 = $data[56]; $CERTNO9 = $data[57]; $CERTSHRS9 = $data[58]; $CERTNO10 = $data[59]; $CERTSHRS10 = $data[60]; $FILLER_B = $data[61]; #print "$CERTNO10"; # build insert string with items in @cols my $sql = ("INSERT INTO mf_forms (CLIENT,RECORD,CONTROL,CUSIP,FRO +MACCT,F_IND,TOACCT,T_IND,". "FP,ORIGQTY,DIV,LTCG,STCG,SOCSEC,BKRNO,THIRD +PARTYCD,USERID,EDATE,". "ORIGAMT,CLOSEDIND,ADPSECNO,BRNCH,ACNT,TYPE,C +HKDIGIT,PDATE,ACTLQTY,". "ACTLAMT,CERTIND,REQST,RDATE,COMNTS,TRNFTYPE,F +ILLER1,ADR_L1,ADR_L2,ADR_L3,". "ADR_L4,ADR_L5,ADR_L6,FILLER_B,CERTNO1,CERTSHRS +1,CERTNO2,CERTSHRS2,CERTNO3,". "CERTSHRS3,CERTNO4,CERTSHRS4,CERTNO5,CERTSHR +S5,CERTNO6,CERTSHRS6,CERTNO7,CERTSHRS7,". "CERTNO8,CERTSHRS8,CERTNO9,CERTSHRS9,CERTNO10 +,CERTSHRS10,FILLER_B) VALUE ('". $CLIENT,$RECORD,$CONTROL,$CUSIP,$FROMACCT,$F_IN +D,$TOACCT,$T_IND,". $FP,$ORIGQTY,$DIV,$LTCG,$STCG,$SOCSEC,$BKRNO,$ +THIRDPARTYCD,$USERID,$EDATE,". $ORIGAMT,$CLOSEDIND,$ADPSECNO,$BRNCH,$ACNT,$TY +PE,$CHKDIGIT,$PDATE,$ACTLQTY,". $ACTLAMT,$CERTIND,$REQST,$RDATE,$COMNTS,$TRNFTYP +E,$FILLER1,$ADR_L1,$ADR_L2,$ADR_L3,". $ADR_L4,$ADR_L5,$ADR_L6,$FILLER_B,$CERTNO1,$CERT +SHRS1,$CERTNO2,$CERTSHRS2,$CERTNO3,". $CERTSHRS3,$CERTNO4,$CERTSHRS4,$CERTNO5,$CERTSHR +S5,$CERTNO6,$CERTSHRS6,$CERTNO7,$CERTSHRS7,". $CERTNO8,$CERTSHRS8,$CERTNO9,$CERTSHRS9,$CERTNO1 +0,$CERTSHRS10,$FILLER_B)"); $dbh->do( $sql ) or die "Couldn't insert record. $!\n"; } close(INFILE); $dbh->disconnect;
Edit: davorg - added code tags

Replies are listed 'Best First'.
Re: Problem with code
by grinder (Bishop) on Jan 31, 2003 at 15:10 UTC

    That's a fierce insert statement to reparse each time. With Oracle you might be lucky because it will find it in its library of recently parsed statements, but all the same...

    You want to prepare this statement ahead of time, with something like the following:

    my $sth = $db->prepare( qq{ INSERT INTO mf_forms (CLIENT,RECORD,CONTROL,CUSIP,FROMACCT,F_IND,TOACC +T,T_IND, FP,ORIGQTY,DIV,LTCG,STCG,SOCSEC,BKRNO,THIRDPARTYCD,USERID,EDATE, ORIGAMT,CLOSEDIND,ADPSECNO,BRNCH,ACNT,TYPE,CHKDIGIT,PDATE,ACTLQTY, ACTLAMT,CERTIND,REQST,RDATE,COMNTS,TRNFTYPE,FILLER1,ADR_L1,ADR_L2,ADR_ +L3, ADR_L4,ADR_L5,ADR_L6,FILLER_B,CERTNO1,CERTSHRS1,CERTNO2,CERTSHRS2, CERTNO3,CERTSHRS3,CERTNO4,CERTSHRS4,CERTNO5,CERTSHRS5,CERTNO6,CERTSHRS +6 ,CERTNO7,CERTSHRS7,CERTNO8,CERTSHRS8,CERTNO9,CERTSHRS9,CERTNO10,CERTSH +RS10,FILLER_B ) VALUES ( ?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?) }) or die "Cannot prepare statement: ${\$db->errstr}\n";

    I might note in passing that you had written VALUE, not VALUES, which will cause an SQL syntax error. In your loop, this prepared statement can then be called thusly:

    $sth->execute($CLIENT,$RECORD,$CONTROL,$CUSIP,$FROMACCT,$F_IND,$TOACCT +,$T_IND, $FP,$ORIGQTY,$DIV,$LTCG,$STCG,$SOCSEC,$BKRNO,$THIRDPARTYCD,$USERID,$ED +ATE, $ORIGAMT,$CLOSEDIND,$ADPSECNO,$BRNCH,$ACNT,$TYPE,$CHKDIGIT,$PDATE, $ACTLQTY,$ACTLAMT,$CERTIND,$REQST,$RDATE,$COMNTS,$TRNFTYPE,$FILLER1,$A +DR_L1,$ ADR_L2,$ADR_L3,$ADR_L4,$ADR_L5,$ADR_L6,$FILLER_B,$CERTNO1,$CERTSHRS1,$ +CERTNO2,$CERT SHRS2,$CERTNO3,$CERTSHRS3,$CERTNO4,$CERTSHRS4,$CERTNO5,$CERTSHRS5,$CER +TNO6,$CERTSHR S6,$CERTNO7,$CERTSHRS7,$CERTNO8,$CERTSHRS8,$CERTNO9,$CERTSHRS9,$CERTNO +10,$CERTSHRS10,$FILLER_B) or die "Could not insert: ${\$sth->errstr}\n";

    You will note that it is a bit of a bear to create the INSERT statement, and to make sure that you have sufficient ?s (or placeholders) for your columns being inserted. Be lazy, and get Perl to do it for you:

    my @columns = qw/ CLIENT RECORD CONTROL CUSIP FROMACCT F_IND TOACCT T_IND FP ORIGQTY DIV LTCG STCG SOCSEC BKRNO THIRDPARTYCD USERID EDATE ORIGAMT CLOSEDIND ADPSECNO BRNCH ACNT TYPE CHKDIGIT PDATE ACTLQTY ACTLAMT CERTIND REQST RDATE COMNTS TRNFTYPE FILLER1 ADR_L1 ADR_L2 ADR_ +L3 ADR_L4 ADR_L5 ADR_L6 FILLER_B CERTNO1 CERTSHRS1 CERTNO2 CERTSHRS2 CERTNO3 CERTSHRS3 CERTNO4 CERTSHRS4 CERTNO5 CERTSHRS5 CERTNO6 CERTSHRS +6 CERTNO7 CERTSHRS7 CERTNO8 CERTSHRS8 CERTNO9 CERTSHRS9 CERTNO10 CERTSH +RS10 FILLER_B /; my $sql = 'inset into mf_forms (' . join( ',', @columns ) . ') values (' . join( ',', ('?') x @columns ) . ')';

    This way you eliminate the need to keep the placeholders in sync. Binding the variables remains a problem, but there are ways around that too, although more complex to set in motion. Come to think of it, as you already have an array with the values, you are already set, just pass it as the parameter like this $sth->execute(@data). Easy peasy.

    Uh, hang on, you split to @cols and refer to @data. That's not going to work, you know?


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Problem with code
by Aristotle (Chancellor) on Jan 31, 2003 at 14:15 UTC

    Some error messages would be nice, but one thing that springs to mind is that you don't declare any of your miriad of variables. strict will rightly yell at you for that. You also have lots of incorrectly closed quotes. It isn't very wise to post unobscured passwords on public forums either.

    The following is an untested cleanup. Please do ask about anything you don't understand.

    #!/usr/bin/perl -w use strict; use DBI; my @fields = qw( CLIENT RECORD CONTROL CUSIP FROMACCT F_IND TOACCT T_IND FP ORIGQTY DIV LTCG STCG SOCSEC BKRNO THIRDPARTYCD USERID EDATE ORIGAMT CLOSEDIND ADPSECNO BRNCH ACNT TYPE CHKDIGIT PDATE ACTLQTY ACTLAMT CERTIND REQST RDATE COMNTS TRNFTYPE FILLER1 ADR_L1 ADR_L2 ADR_L3 ADR_L4 ADR_L5 ADR_L6 FILLER_B CERTNO1 CERTSHRS1 CERTNO2 CERTSHRS2 CERTNO3 CERTSHRS3 CERTNO4 CERTSHRS4 CERTNO5 CERTSHRS5 CERTNO6 CERTSHRS6 CERTNO7 CERTSHRS7 CERTNO8 CERTSHRS8 CERTNO9 CERTSHRS9 CERTNO10 CERTSHRS10 FILLER_B ); my $dbh = DBI->connect( "dbi:ODBC:INSTDev", "sa", "password", { RaiseError => 1, PrintError => 1, AutoCommit => 1 } ) or die "Unable to connect: $DBI::errstr\n"; @ARGV = ("/tf_out.txt"); my $sth = do { my $cols = join(',', @fields); my $placeholders = join(',', ('?') x @fields); $dbh->prepare("INSERT INTO mf_forms ($cols) VALUE ($placeholders)" +; }; while(<>) { chomp; my %infields; @infields{+FIELDS} = split /\t/, $_, -1; $sth->execute(@infields{+FIELDS}); } $dbh->disconnect;

    Makeshifts last the longest.

      Can you explain to me what you are doing with my $placeholders = join(',', ('?') x @fields);
        Well, given a list on the left hand side, x will produce an output list which consists of as many copies as you specify with the right hand side operand. The right side is evaluated in scalar context, and we're using an array there; the result is the number of elements in that array. So
        ('?') x @fields

        will produce a list which has exactly as many elements as @fields, where each element is a question mark. So given @fields = qw(foo bar baz) I have created a list like ('?', '?', '?')

        Then I join them using a comma as a separator, giving me, in the previous example, '?,?,?'.

        As explained in detail in the DBI documentation section on placeholders, I do that so I don't have to generate a new SQL statement over and over; instead, I generate a single one, where I let the driver fill in the values I supply to execute().

        Makeshifts last the longest.

Re: Problem with code
by AcidHawk (Vicar) on Jan 31, 2003 at 14:29 UTC

    It looks like you are missing some " marks.. This is what your sql query should look like..

    my $sql = ("INSERT INTO mf_forms (CLIENT,RECORD,CONTROL,CUSIP,FROMACC +T,F_IND,TOACCT,T_IND,". "FP,ORIGQTY,DIV,LTCG,STCG,SOCSEC,BKRNO,THIRD +PARTYCD,USERID,EDATE,". "ORIGAMT,CLOSEDIND,ADPSECNO,BRNCH,ACNT,TYPE,C +HKDIGIT,PDATE,ACTLQTY,". "ACTLAMT,CERTIND,REQST,RDATE,COMNTS,TRNFTYPE,F +ILLER1,ADR_L1,ADR_L2,ADR_L3,". "ADR_L4,ADR_L5,ADR_L6,FILLER_B,CERTNO1,CERTSHRS +1,CERTNO2,CERTSHRS2,CERTNO3,". "CERTSHRS3,CERTNO4,CERTSHRS4,CERTNO5,CERTSHR +S5,CERTNO6,CERTSHRS6,CERTNO7,CERTSHRS7,". "CERTNO8,CERTSHRS8,CERTNO9,CERTSHRS9,CERTNO10 +,CERTSHRS10,FILLER_B) VALUES ('". "$CLIENT,$RECORD,$CONTROL,$CUSIP,$FROMACCT,$F_I +ND,$TOACCT,$T_IND,". "$FP,$ORIGQTY,$DIV,$LTCG,$STCG,$SOCSEC,$BKRNO, +$THIRDPARTYCD,$USERID,$EDATE,". "$ORIGAMT,$CLOSEDIND,$ADPSECNO,$BRNCH,$ACNT,$T +YPE,$CHKDIGIT,$PDATE,$ACTLQTY,". "$ACTLAMT,$CERTIND,$REQST,$RDATE,$COMNTS,$TRNFTY +PE,$FILLER1,$ADR_L1,$ADR_L2,$ADR_L3,". "$ADR_L4,$ADR_L5,$ADR_L6,$FILLER_B,$CERTNO1,$CER +TSHRS1,$CERTNO2,$CERTSHRS2,$CERTNO3,". "$CERTSHRS3,$CERTNO4,$CERTSHRS4,$CERTNO5,$CERTSH +RS5,$CERTNO6,$CERTSHRS6,$CERTNO7,$CERTSHRS7,". "$CERTNO8,$CERTSHRS8,$CERTNO9,$CERTSHRS9,$CERTNO +10,$CERTSHRS10,$FILLER_B)");
    what is happening is that the last ; is not being seen as inside "".

    And you need to declare all thos e variables.. put the worh my in fron of each var.. something like my $CLIENT = $data[0];

    Update: Along with putting a my in front of all tha vars you will need to add my (@data); at the top of your code somewhere for all the elements you use in each of the my $CLIENT = $data[0]; lines.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: Problem with code
by physi (Friar) on Jan 31, 2003 at 14:39 UTC
    sure your sql-statement must not be written:
     "insert into mf_forms (....) values (...)"

    I think it must be 'values' not 'value'

    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Problem with code
by Jaap (Curate) on Jan 31, 2003 at 14:06 UTC
    What are the error messages you get?

    P.S. next time use <code></code> tags around your code please, for readibility.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://231595]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found