?
Solved

Can't input data to MySQL from PERL script

Posted on 2004-04-13
2
Medium Priority
?
657 Views
Last Modified: 2012-05-04
Hi gurus


So I have a problem sending data to MySQL database. Here is my PERL test script:


---------------------- SCRIPT START ----------------------

#!/usr/bin/perl -w


use Net::MySQL;
use strict;
use IO::Socket;
srand;

my $i=0;

my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        database => 'test',
        user => 'root',
        password => ''
);

#INSERT

for ($i=1; $i < 4 ; $i++)
{
print "Line number:\t $i\n";

my $a=int(rand(10))+1;
my $b=int(rand(16))+1;
my $c=int(rand(34))+1;

$mysql->query(q{
INSERT INTO testi (eka, toka, kolmas) values ($a, $b, $c)
});
$mysql->close;
}


---------------------- SCRIPT END ----------------------

And the errors iut gives me when I try to run it:


---------------------- ERROR ----------------------


-bash-2.05b$ ./PERLTESTI
Line number:     1
Line number:     2
getpeername() on closed socket GEN0 at /usr/lib/perl5/5.8.0/i386-linux-thread-multi/IO/Socket.pm line 199.
send: Cannot determine peer address at /usr/lib/perl5/site_perl/5.8.0/Net/MySQL.pm line 277


---------------------- ERROR END ----------------------


I can input data to database from the >MySQL command line so its works fine.


Versions that are in use:
mysql  Ver 12.22 Distrib 4.0.18, for pc-linux (i686)
perl, v5.8.0 built for i386-linux-thread-multi



Any help ?


- Jussi
0
Comment
Question by:salmjuh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
amandeep earned 500 total points
ID: 10811599

Below is the modified code:

==============================
#!/usr/bin/perl -w

use Net::MySQL;
use strict;
use IO::Socket;
srand;

my $i=0;

my $mysql = Net::MySQL->new(
        hostname => '127.0.0.1',
        database => 'test',
        user => 'root',
        password => ''
);

#INSERT

for ($i=1; $i < 4 ; $i++) {
      print "Line number:\t $i\n";
      my $a=int(rand(10))+1;
      my $b=int(rand(16))+1;
      my $c=int(rand(34))+1;
      $mysql->query(qq{
            INSERT INTO testi (eka, toka, kolmas) values ($a, $b, $c)
      });
}

$mysql->close;
==============================

0
 
LVL 8

Expert Comment

by:davorg
ID: 10817591
You should probably check the return value from the "new" call. It's possible that you are simply failing to connect to the database.

Is there a good reason why you're not using DBI and DBD::mysql?

Dave...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question