473,624 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting the column names of the table in DB testing

11 New Member
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 3067
numberwhun
3,509 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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 Recognized Expert Specialist
@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
2146
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 'Nachname'. 'ra' takes place within a word. This is solved with regular expression. So that I can limit my search. For example: I can search for 'ra' and it is not relevant wich letters follow or wich letters are in front of 'ra'. Now I want to...
8
64818
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 User_Lecturer L, User_Student S, TBL_Intake I, Users U 4 WHERE L.UserID=S.InitialSupervisorID And L.UserID=U.UserID And
14
13429
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 strings) at least 12 digits (characters) long - all positions will be occupied (no leading 0's) - Tables may have upto 1m+ rows
10
6557
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 with SQL. I'd like to take the name of the column and input it into a descritor field. This isn't the table, but will serve as a better illustration than the real deal. If the table looks like this:
2
23910
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 but the table column names. I've seen other posts that suggest using the SQL command DESCRIBE but I can't get it to work for some reason. Other posts have code samples but they're written in VB which I am not familiar with. I
4
2975
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 the user selects. I could take the brute force method and hard code the column names but then every time I add a new table or modify an existing one I would have to modify the code. What I want is to have a generic function that given the table...
2
10388
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 actually happens is... when the Text size is too big... the column
6
35567
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 -Id -FirstName -LastName
1
24251
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 width will be necessary. How do I tell the dropdown (<SELECT>) and/or column (<TD>) to automatically size itself to the greater of widest item in the dropdown, and then get the smaller dropdowns to fill the width of the column? If I specify in...
0
8679
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.