Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can't input data to MySQL from PERL script

Posted on 2004-04-13
2
Medium Priority
?
664 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
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…
Six Sigma Control Plans

618 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