473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question on DBI module?

25
Hi,
I am interacting with the database through use DBI module. Please take a look at the follwoing:

Expand|Select|Wrap|Line Numbers
  1. #I am querying the database for the name and age information
  2.  
  3. $sth = $dbh->prepare(q{SELECT name, age FROM group});
  4.  
  5. #Using the while loop I am printing the information on the screen:
  6.  
  7. my @row;
  8. while (@row = $sth->fetchrow_array()){
  9.     print "ROW:@row\n";
  10. }
  11.  
I am seeing the information of name and age on the output screen (which I got from querying the db) when I run this program.
But I want to store the same information in a file.

Can anyone please let me know how to store the same information to a file.

Thanks in advance!
Sangith
Jun 14 '07 #1
5 1146
miller
1,089 Expert 1GB
Sangith,

This question isn't about the DBI module at all. It's about outputting to a file. There are many ways to do this, what type of spec are you trying to satisfy?

cpan Text::CSV

Expand|Select|Wrap|Line Numbers
  1. use Text::CSV;
  2.  
  3. use strict;
  4.  
  5. ...
  6.  
  7. my $outfile = 'group.csv';
  8.  
  9. my $sth = $dbh->prepare(q{SELECT name, age FROM group});
  10. $sth->execute or die $dbh->errstr;
  11.  
  12. open(OUT, ">$outfile") or die "Can't open $outfile: $!";
  13. $csv = Text::CSV->new();
  14.  
  15. while (my @row = $sth->fetchrow_array()){
  16.     if ($csv->combine(@row)) {
  17.         print OUT $csv->string(), "\n";
  18.     } else {
  19.         die "combine() failed on argument: ", $csv->error_input, "\n";
  20.     }
  21. }
  22.  
  23. close(OUT);
  24.  
  25. $sth->finish; undef $sth;
  26.  
- Miller
Attached Files
File Type: txt scratch.txt (487 Bytes, 359 views)
Jun 14 '07 #2
sangith
25
Hi Miller,

I totally understand that outputting to a file is completely independant of DBI module. That can be done in various ways. Using Text::CSV, or can use a file handle to output to an output file, etc

I appreciate your quick response! The program you sent me works just fine. I have just started using Perl modules in my program. I have few questions related to Modules.

I was reading "Programming Perl" book to get a good knowledge on Modules. But they are talking about object construction, class Inheritance, Overloading, Tied variables,......That looks very complicated to me.

I am not going to create modules/Packages. I am just going to use different modules in my Perl programs.
So for using the modules in my program, what should I know and what should I read about?
Because although I use modules, I don't really understand what I am passing to the modules, what '->' means ,..........(Its totally not clear)
Can you please point me to any document/resources on using Modules.
I would appreciate your advice!

Thanks in advance!
Sangith
Jun 15 '07 #3
KevinADC
4,059 Expert 2GB
Most modules have documentation that explains how to use them. Some explain better than others. You can look up modules by their name on cpan:

http://search.cpan.org/
Jun 15 '07 #4
sangith
25
Most modules have documentation that explains how to use them. Some explain better than others. You can look up modules by their name on cpan:

http://search.cpan.org/

Thank you for your response.

-Sangith
Jun 15 '07 #5
miller
1,089 Expert 1GB
I totally understand that outputting to a file is completely independant of DBI module. That can be done in various ways. Using Text::CSV, or can use a file handle to output to an output file, etc
I chose to demonstrate the Text::CSV module to you simply because if you are going to output records to a file, it makes sense that you be able to load them easily again. A csv file is a very standard format, so could easily be imported into many other applications, such as a Spreadsheet for example.

I have few questions related to Modules.
...
So for using the modules in my program, what should I know and what should I read about?
...
Can you please point me to any document/resources on using Modules.
I would appreciate your advice!
As Kevin pointed out, cpan is often the best place for documentation about modules that you will use. Another resource is perldoc:

http://perldoc.perl.org/

There are a lot of Core Modules that are included in perl, so it's good to at least be aware of them if not familiar. There are also a lot of tutorials available along with other documentation. So perldoc is typicaly the first place I look for information.

Good Luck.
- Miller
Jun 15 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kevin MacKenzie | last post by:
I'm a complete newbie to using Python. I have a small question about importing modules. Is there any difference between the two following statements, and what (if any) are they? >>> from...
2
by: Aubrey Hutchison | last post by:
Question about functions: 1)--- module "A" contains the following X = 999666 #easy to notice value Y = 111222 #easy to notice value Product = 69696969 #easy to...
0
by: David M. Wilson | last post by:
Hello! I maintain a small package for talking to the API of BulkSMS.co.uk. I have been adding support for some new features recently, and found myself slightly indecisive over how best to lay...
18
by: Amir Dekel | last post by:
This must be the silliest question ever: What about user input in Python? (like stdin) Where can I find it? I can't find any references to it in the documentation. Amir
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
6
by: Tuvas | last post by:
I know this is probably a very simple question, but I am building a program that is now at about 2400 lines of code in the main module. I need to break it up, however, there are certain variables...
0
by: Rote Rote | last post by:
I have an Xml file and XSL o format it below. I have tried to distinct the "ProgramCode" and the "programDescription" attributes but after that i still get repeatable data of...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
2
by: warhero | last post by:
Hey all, sorry for the totally newb question. I recently switched over to python from ruby. I'm having problems figuring out how module importing works.. as a simple example I've got these files: ...
0
by: Gary Herron | last post by:
Dan Yamins wrote: Because loading (and reloading) assigns values to variables (called binding a value in Python), but does not go on a hunt to find variables to *unbind*. Once a variable is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.