LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-05-2005, 08:12 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 31
Mysql reading via Perl DBI


I use the DBI module and I wander if my way to retrieve data from tables is good.
There are a few rows in the table, I scan them all and build an array from each of it:
Code:
my $sth = $dbh->prepare ("SELECT date FROM msgs");
$sth->execute();
while (@a_date = $sth->fetchrow_array){
push(@dates,@a_date)}

$sth = $dbh->prepare ("SELECT nick FROM msgs");
$sth->execute();
while (@a_nick = $sth->fetchrow_array){
push(@nicks,@a_nick)}

$sth = $dbh->prepare ("SELECT msg FROM msgs"); 
$sth->execute();
while (@a_msg = $sth->fetchrow_array){
push(@msgs,@a_msg)}
I have 3 arrays: dates, nicks and msgs that holds the whole info from the table, but that seems kind of slow to me. Is there a better way to do that job in Perl (with DBI module) ?
 
Old 02-06-2005, 10:46 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
As usual, TIMTOWTDI, but this would work:

Code:
$sth = $dbh->prepare ("SELECT date, nick, msg FROM msgs");
$sth->execute();
while( $date, $nick, $msg) = $sth-fetchrow() )
{
    $info{$nick} = [ $date, $msg ];
}
creates a hash keyed on nick. Depends what you want to do with the data really.
The "while()" line could be
while( @p_info = $sth->fetchrow() )
for the same SQL as I've used.... and do some thing different with the hash assignment line...

HTH
 
Old 03-21-2007, 04:03 AM   #3
Sunil Kartikey
LQ Newbie
 
Registered: Mar 2007
Location: Pune
Posts: 4

Rep: Reputation: 0
preparing a query is a relatively expensive operation.
if you have single table .. select * from the table fetch all data inside a hash or array and use it where ever required.. no need to prepare saperately for each column date nick and msg.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Perl DBI question GoTrolling Linux - Newbie 1 03-21-2007 04:31 AM
Perl - DBI PG Installation Problems TheMeteorPolice Linux - Software 2 11-23-2005 10:53 AM
perl | DBI->connect | RHEL AS 3 kaN5300 Linux - Enterprise 6 09-14-2004 08:15 AM
perl mysql DBI 400 character limit? tjtoocool Linux - Software 2 01-10-2004 04:44 AM
perl DBI prepare question mrtwice Programming 1 10-30-2003 02:12 PM


All times are GMT -5. The time now is 11:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration