use strict; use warnings;
and use placeholders in your sql. Put a ? in your sql where you would liek to put a value, then when you call execute pass the values.

#!/usr/bin/perl # This script reads a file and creates 2 variables with it by using a +hash. # the data is in 2 columns. Column1=msisdn Column2=DeletionDate # This script may be helpful if expanded to a loop and db query for fi +guring # last possible active date. # # Sample of the file read by script # 19992507638 deletions.20060723 # 19993017551 deletions.20060723 use strict; use warnings; use DBI; my $database = "chai"; my $hostname = "localhost"; my $port = "3306"; my $username = "bonezer"; my $password = 'monezer'; my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; my $dbh = DBI->connect($dsn, $username, $password) or die("Could not c +onnect!"); my $sql = "select to_days(substring( ?,11)) - to_days(lastactive) fro +m user where number = ? into OUTFILE '/tmp/sp'"; my $sth = $dbh->prepare($sql); open(DATA, "/tmp/SEOUL_NUMBERS") or die "Failed to open /tmp/SEUL_NUMB +ERS file: $!"; my %hash; while( <DATA> ) { @elements = split / /, $_; $hash{ $elements[0] } = $elements[1]; $hash{ $elements[1] } = $elements[1]; $sth->execute($elements[1], "$elements[0]\n"); } $dbh->disconnect;

It's untested, but hopefully you can see the use of strict, warnings, and placeholders as well as prepared SQL statements. Cheers!


___________
Eric Hodges

In reply to Re: Looping an SQL query by eric256
in thread Looping an SQL query by rickphil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.