By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,390 Members | 2,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,390 IT Pros & Developers. It's quick & easy.

getting the column names of the table in DB testing

P: 11
how to get the column names and the values of them in select statment.

I have a packaze like this named ADB.pm

Expand|Select|Wrap|Line Numbers
  1. package ADB;
  2.  
  3. use DBI;
  4.  
  5. @ISA = ('Exporter');
  6. @EXPORT_OK = ("Connection_check", "new","select_db","insert_db","delete_db","update_db");
  7.  
  8. sub new
  9. {
  10.     my $class = shift;
  11.     my $self = {
  12.         _pf => shift,
  13.         _db => shift,
  14.         _host => shift,
  15.         _port => shift,
  16.         _user => shift,
  17.         _pass => shift
  18.     };
  19.     $dbh = Connection_check($self);
  20.     #$self->{_dbh} = $dbh;
  21.     bless $self, $class;
  22.     return $self;
  23. }
  24.  
  25. sub Connection_check {
  26.     my($self ) = @_;
  27.     $dsn = "DBI:$self->{_pf}:$self->{_db};$self->{_host};$self->{_port}";
  28.     #$dsn = "DBI:mysql:database=$self->{_db};host=$self->{_host};port=$self->{_port}";
  29.     $dbhc = DBI->connect($dsn,$self->{_user},$self->{_pass})or die "Unable to connect: $DBI::errstr\n";
  30.     return ($dbhc)
  31. }
  32.  
  33. ### Retrieving the Rows ########
  34.  
  35. sub select_db{
  36.   my ($obj,$sel) = @_;
  37.   my $selnew = $dbh->prepare($sel);
  38.   $selnew->execute or die "Unable to connect: $DBI::errstr\n";
  39.   my @AoA;
  40.   my @FinArr;
  41.   while (@AoA = $selnew->fetchrow_array)
  42.     {
  43.       push @FinArr, [@AoA];
  44.     }
  45.  
  46.   return @FinArr;
  47. }
  48.  
and perl file adb.pl like this

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use DBI;
  4. use DBD::mysql;
  5. use Utils::ADB;
  6.  
  7.  
  8. $object = new ADB( "mysql","adb", "192.168.106.218", 3306, "root", "root");
  9.  
  10. my @records = $object -> select_db ("select * from users");
  11.  
  12. foreach $recRef (@records) {
  13.   print join ("   ", @$recRef),"\n";
  14. }
  15.  
  16. print "\n \n";
  17.  
when i execute adb.pl it displays the table results blidly. i need to get the column names also with the respective values. Can u please tell me which modification i need to do in the code
results what i am getting are like this
1 admin admin 1 3 3
2 demouser 111111 1 1 1


i want the column names to be print for them.
Dec 10 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
You aren't really going to be able to retrieve the column names from the select statement. Instead, you are going to have to do another db query statement to do either a "describe <tablename>" or a "show columns from <tablename>". They both do the same thing. You can then write a bit of code to parse out the column names so you can print them before your data is printed.

Regards,

Jeff
Dec 10 '08 #2

eWish
Expert 100+
P: 971
I would suggest that you define the table names rather than using the *. This way you are only getting the data from the columns you need.

--Mork
Dec 13 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
@eWish
naa-noo naa-noo :)
Dec 13 '08 #4

Post your reply

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