Link to home
Start Free TrialLog in
Avatar of Tereza
TerezaFlag for United States of America

asked on

Convert perl mysql script to Ruby

Im trying to learn Ruby and do a simple conversion from perl...my issue is I know how to select the data and  iterate through the data ..however I want to take that result and have it feed a insert statement. Anyone give me an example

 sth = dbh.execute("SELECT X from from XY")
       while row = sth.fetch do
HERE

Avatar of cminear
cminear

While very basic, and doesn't necessarily cover the issues you may find, do check the 'examples' directory from ruby DBI (which would be in the gems directory if you installed it as a gem).  It has a test script that is written in perl and ruby, so you can learn a bit from that.

Also, the documentation for ruby DBI should have been installed with a gem, but you can use http://ruby-dbi.rubyforge.org/rdoc/index.html if that would be more convenient.

With that out of the way, here is an example which should be a framework for how to add inserts.  First we prepare the insert statement that will be executed for each record returned from the SELECT.  (I just made a guess.)  Then, you execute the SELECT and use a block format so that the statement handle is handed right into the block.  Then similar to what you had, you fetch each row from the SELECT query, which is assigned to the 'row' variable in the block.  With that value, we do an 'execute' on the 'insert_stmt' we created at the beginning.  You also need to send 'finish' to insert_stmt in order to allow the database handle to be disconnected cleanly.
insert_stmt = dbh.prepare("INSERT INTO yz (<your column>) VALUES (:1)")

dbh.execute("SELECT x FROM xy") {|sth|
  sth.fetch {|row|
    insert_stmt.execute(row[0])
  }
}

insert_stmt.finish

Open in new window

Avatar of Tereza

ASKER

cminear
thanks for your help...this is a simple section of my perl script...

sub iptarget {

print "Enter the number of ips in each ip range to generate : \n";
       chomp($numrows=<STDIN>);
        
#Select ipfrom from the ip2locsector table as starting ip
$dbquery = "SELECT ipfrom from ip2locsector";
$sth1 = $db ->prepare($dbquery);
$sth1->execute() or die "SQL Error: $DBI::errstr\n";

#Get the starting value of each range of ip addresses
while (@ipfrom = $sth1->fetchrow_array)
  {  
  $count=0;
    foreach $ipfrom (@ipfrom)
       {
        # get scalar values from the array, set startip to each
             $startip=$ipfrom;
             #  print $startip . "\n";
 # Loop through each value $numrows adding 1 to the IP address and insert into ips table              
          while ($count<$numrows)
               {
                    $startip++;
              $query  = "INSERT INTO ips (ip) VALUES ('$startip')";
              $sth = $db->prepare($query);
              $sth->execute();
           #   print $query . "\n";
                    $count++;
                  }  
        }
  }
  exit;
}

******************************************************************************************
THE SELECT AND HAVING IT IN AN ARRAY SEEMS  STRAIGHT FORWARD BUT WHAT ABOUT THE LOOPING AND ANY ALTERS TO DATA....iM VERY NEW TO THIS...IVE ALSO LOOKED AT ACTIVERECORD ....CAN YOU HELP WITH THIS SECTION....IM GOING TO CHECK THE SITES YOU SUGGESTED AS WELL
ASKER CERTIFIED SOLUTION
Avatar of cminear
cminear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial