Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 8th, 2001, 05:56 AM
608zDcom 608zDcom is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: uk
Posts: 2 608zDcom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to 608zDcom
Question Query MySQL with Perl

I want to query a mysql table using perl.
I want to issue this command upon connecting:
'select * from MODEMS;' and read the output to an array/hash variable.


So far, I have the following code:
-----------------------------------------------------------
use DBI;
use Socket;
my $database_name = "rota1";
my $location = "localhost";
my $port_num = "3306";
my $database = "DBI:mysql:$database_name:$location:$port_num";
my $db_user = "luser";
my $db_password = "lusers-password";
my $dbh = DBI->connect($database,$db_user,$db_password);

$sth->finish;
$dbh->disconnect;
------------------------------------------------------------

What do you think I am missing?
Where shall I put the mysql statement?

thankyou all.

Reply With Quote
  #2  
Old August 9th, 2001, 05:32 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
ok..

i havent used mysql with perl but still, i would say that onceu have made connection using

my $dbh = DBI->connect($database,$db_user,$db_password);

u would have one variable that is $dbh associated with ur connection. now use this $dbh to query ur database.


so there can be some method like query. i am not sure.

check doc about DBI.
jd
__________________
_____________________________
d.k.jariwala (JD)
~ simple thought, simple act ~
I blog @ http://jdk.phpkid.org

Reply With Quote
  #3  
Old August 9th, 2001, 09:14 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
You're so close!

You use the $dbh connection scalar to prepare, execute and use other DBI methods.

(BTW, I haven't checked the syntax of your connection. This is all simple documentation stuff, and there are tutorials for working with DBI all over the place. Talk to my best friend, www.google.com)

So, to prepare and execute a query once your connection is setup properly-
Code:
# Create the query
my $query=$dbh->prepare('select * from table1 group by field1 order by field2') 
     or die('Couldn\'t prepare query:',$dbh->errstr());
#Do the query
$query->execute() or die('Couldn\'t execute query:',$dbh->errstr());

#Do something with what's retrieved from the query
while(my($field1,$field2,$field3)=$query->fetchrow_array()){
    #Stuff is done with the fields retrieved here.
}
$query->finish();


The most important thing, in my opinion, is to status check every call to DBI through $dbh or other DBI objects. If something fails, it will tell you exactly what happened and make you debugging MUCH easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Query MySQL with Perl


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT