CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
storing values to mySQL - with DBI

 



dilbert
User

Jun 7, 2014, 4:31 AM

Post #1 of 4 (6231 views)
storing values to mySQL - with DBI Can't Post

 
how to put the output of the request to the db

have perl :: DBI installed
also the mysql-db is up and running




https://metacpan.org/source/LBROCARD/Parse-CPAN-Authors-2.27/README


use Parse::CPAN::Authors;

# must have downloaded
my $p = Parse::CPAN::Authors->new("01mailrc.txt.gz");
# either a filename as above or pass in the contents of the file
my $p = Parse::CPAN::Authors->new($mailrc_contents);

my $author = $p->author('LBROCARD');
# $a is a Parse::CPAN::Authors::Author object
# ... objects are returned by Parse::CPAN::Authors
print $author->email, "\n"; # leon@astray.com
print $author->name, "\n"; # Leon Brocard
print $author->pauseid, "\n"; # LBROCARD

# all the author objects
my @authors = $p->authors;




i want to store all the output in the mysql-db




regarding the db-things: well i am pretty new to perl-tasks.

but with the above mentioned module - i think i can learn alot - it is quite very simple. And with this i can play around - and try to find out how to store the data into a mysql db.


well could the results that i get be regarded as a perl object (reference to array of references) like the below:

Code

my $a = [ [$a, $ab, $c ], [$a, $b, $c] ] ;


and need to store it on the DB then retrieve it.

i look for a good mechanism to serialize it and then store it on the DB?


hmm - if i want to store:

Code

use Storable
use DBI;

# ... connect to database
# Store
my $data = [ [$a, $b, $c ], [ $a, $b, $c ] ];
my $bytestream = nfreeze $data;
$dbh->do('insert into table (field) values(?)', undef, $bytestream);


by the way: What about Data::Dumper?


FishMonger
Veteran / Moderator

Jun 7, 2014, 6:53 AM

Post #2 of 4 (6131 views)
Re: [dilbert] storing values to mySQL - with DBI [In reply to] Can't Post

There's no reason/need to use the Storable module if you're planning on putting the data into a database.

@authors is an array of objects. You need to loop over that array and insert each author's info into the DB.

You could use the Data::Dumper module to inspect/review the structure of the @authors array. For example, if you add a print Dumper \@authors; statement your output would look like this:

Code
acme@astray.com 
Leon Brocard
LBROCARD
$VAR1 = [
bless( {
'name' => 'Neil Hainer',
'email' => 'CENSORED',
'pauseid' => 'NHAINER'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'CROSSWIRE',
'email' => 'matt@crosswire.com',
'name' => 'Matthew Sibley'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'DAVIDJNSN',
'name' => 'David Jensen',
'email' => 'CENSORED'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'KESZLER',
'name' => 'Scott R. Keszler',
'email' => 'keszler@srkconsulting.com'
}, 'Parse::CPAN::Authors::Author' ),


This is how you'd loop over the array.

Code
for my $author ( @authors ) { 
print $author->email, "\n";
print $author->name, "\n";
print $author->pauseid, "\n\n";
}

But instead of printing the data, you would insert it into the db.

The DB table will need the 3 fields to hold the author's ID, name, and email address.

Have you created the database?
Do you know how to connect to the database from your script?
Do you know how to write the insert statement?

Reading over the DBI and DBD::mysql module documentation will answer those questions.


(This post was edited by FishMonger on Jun 7, 2014, 6:59 AM)


dilbert
User

Jun 7, 2014, 1:02 PM

Post #3 of 4 (5852 views)
Re: [FishMonger] storing values to mySQL - with DBI [In reply to] Can't Post

hello dear fishmonger

many thank for the great hints. i am happy to hear from you



In Reply To
There's no reason/need to use the Storable module if you're planning on putting the data into a database.

@authors is an array of objects. You need to loop over that array and insert each author's info into the DB.

You could use the Data::Dumper module to inspect/review the structure of the @authors array. For example, if you add a print Dumper \@authors; statement your output would look like this:

Code
acme@astray.com 
Leon Brocard
LBROCARD
$VAR1 = [
bless( {
'name' => 'Neil Hainer',
'email' => 'CENSORED',
'pauseid' => 'NHAINER'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'CROSSWIRE',
'email' => 'matt@crosswire.com',
'name' => 'Matthew Sibley'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'DAVIDJNSN',
'name' => 'David Jensen',
'email' => 'CENSORED'
}, 'Parse::CPAN::Authors::Author' ),
bless( {
'pauseid' => 'KESZLER',
'name' => 'Scott R. Keszler',
'email' => 'keszler@srkconsulting.com'
}, 'Parse::CPAN::Authors::Author' ),


This is how you'd loop over the array.

Code
for my $author ( @authors ) { 
print $author->email, "\n";
print $author->name, "\n";
print $author->pauseid, "\n\n";
}

But instead of printing the data, you would insert it into the db.

The DB table will need the 3 fields to hold the author's ID, name, and email address.

Have you created the database?
Do you know how to connect to the database from your script?
Do you know how to write the insert statement?

Reading over the DBI and DBD::mysql module documentation will answer those questions.



great great hints -and valuable asset of information


btw: i can use the furhter information from the site

http://search.cpan.org/dist/DBD-mysql/lib/DBD/mysql.pm


Code
 use DBI; 

$dsn = "DBI:mysql:$database";
$dsn = "DBI:mysql:database=$database;host=$hostname";
$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";

$dbh = DBI->connect($dsn, $user, $password);


well i know that i need to have three fields.

i am pretty sure that i will get this done.... i will play around with the code at the weekend and come back and show you all the results.


again many thanks for the hints

greetings
dilbert


dilbert
User

Jun 9, 2014, 2:26 AM

Post #4 of 4 (5510 views)
Re: [dilbert] storing values to mySQL - with DBI [In reply to] Can't Post

found a very very good tutorial that shows how easy that one can be made with php

http://dalibor.dvorski.net/downloads/docs/InstallingConfiguringDevelopingWithXAMPP.pdf


it is very very simple - in PHP. i am going to see if we can do such an easy solution in perl - which is in general a bit more difficult than php - (my opinion)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives