Solved

Can't input data to MySQL from PERL script

Posted on 2004-04-13
2
653 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 125 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

Technology Partners: 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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

688 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