Solved

Can't input data to MySQL from PERL script

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now