473,395 Members | 1,941 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,395 software developers and data experts.

getting the column names of the table in DB testing

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
3 3042
numberwhun
3,509 Expert Mod 2GB
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
971 Expert 512MB
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
4,059 Expert 2GB
@eWish
naa-noo naa-noo :)
Dec 13 '08 #4

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

Similar topics

4
by: Wiebke Pätzold | last post by:
Hi all! I create a database that contains a table. 'Nachname' is one of 13 column names. This program can search for a special letter. In my example it is 'ra'. and the search takes place in...
8
by: Shino | last post by:
Hi, Can anyone help with this error: "ORA-00904: invalid column name"? Thanks! SQL> create view PPFa as 2 SELECT L.UserID AS LecID, U.Name, U.Email, I.IntakeID, S.UserID AS StudID 3 FROM...
14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
4
by: Scot L. Harris | last post by:
Currently using Postgresql 7.2.4-5.80 with php 4.2.2.-8.0.8 on a redhat 8.0 system. I am writing some php scripts where I want to generate a list of the column names in a particular table that...
2
by: Charleees | last post by:
Hi all, I have a DataGrid with Template Columns..... There are LAbels,Linkbuttons in the Single Row.. I have to set the Constant Column width for those Template Columns in Grid... Wat...
6
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees...
1
by: RichardR | last post by:
I have a webpage which has a table that contains a column with several drop down boxes (<SELECT>). The contents of the drop down boxes are dynamically populated so I have no idea what the actually...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.