List:General Discussion« Previous MessageNext Message »
From:Curtis Maurand <> Date:July 18 2003 5:23pm
Subject:Re: Select via Perl
View as plain text  
#!/usr/bin/perl

use DBI;

my $database = "desired database";
my $dbhost = "desired database host";
my $username = "your username";
my $password = "your password";
my $data_source = "DBI:mysql:$database:$dbhost";
my $dbh = DBI->connect($data_source, $username, $password);
my $sourcefile = "the name and path of the datafile you're trying to read";
my $sourcevar;
my $dbquery = "select * from table where varialbe like %$sourcevar%";
my $resultset;
my $count;
open (MYSOURCE, "<$sourcefile");

while (<MYSOURCE>)
  {
   $sourcevar = $_;
   $sth->prepare($dbquery);
   $sth->execute;

   while ($resultset = $sth->fetchrow())
     {
       if ($resultset eq $sourcvar)
        {
           # do something
        }
       else
        {
          # do something else
        }
      $sth->finish;
     }
  }
$dbh->disconnect;
close(MYSOURCE);
# we're done.

Its not very efficient.  It'll execute a lot of select statements, but it'll 
do what you want.

if you're "%var%" is only one select, then you fetch it all into and array and 
recursively search through the array (memory permitting.)  You could also 
parse your source file into an array and run through both arrays recursively.  
I'm just not doing this here.

$myresult = $sth->fetchall_arrayref();
  while (<MYSOURCE>);
    {
      $sourcevar = $_;
      for ($count .. $#{$myresult})
        {
          if ($sourcevar eq $myresult[$count])
           {
             # do something
           }
        }
    }

$sth->finish;
$dbh->disconnect;
close(MYSOURCE);
# we're done




On Friday 18 July 2003 04:21, Rudy Metzger wrote:
> Can you please post the source code?
>
> In any case, I would consider using DBI.
>
> Cheers
> /rudy
>
> -----Original Message-----
> From: Ashwin Kutty [mailto:akutty@stripped]
> Sent: donderdag 17 juli 2003 17:12
> To: mysql@stripped
> Subject: Select via Perl
>
> I am trying to read a file and see if the contents of it exists in the
> DB
> or not and am trying to do it via a Select.
>
> I try to do a select * into outfile /tmp/result.txt from table where
> field
> like "%$var%"; but it always goes through the first two lines of the
> input
> file and then says the file already exists.
>
> Is this because I read each line of a file in a for loop in perl and the
> mysql query tries to recreate the outfile each time?  Is there any other
> way I can dump the results of the select to a file of some sort?
>
> Thanks..
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=1
Thread
Select via PerlAshwin Kutty17 Jul
  • Re: Select via Perlgerald_clark17 Jul
RE: Select via PerlRudy Metzger18 Jul
  • RE: Select via PerlAshwin Kutty18 Jul
  • Re: Select via Perl (Curtis Maurand)21 Jul