Solved

Can't input data to MySQL from PERL script

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

839 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