Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Monks

Below is my code to execute any select Query and fetch data from database and return the fetched value. It will return only the values, Is it possible to take the column name? So that I can hold the column name and the corresponding value in a hash.

sub Select_Qry{ ($class,$dbaseh,$query)=@_; my($sth, $execu); if(!defined($query)){ $sth->finish(); $dbaseh->disconnect(); } if($dbaseh->ping){ $sth=$dbaseh->prepare("$query") or warn $file->log( + level => 'info', message => localtime() .": Can't prepare SQL state +ment:[$query] $DBI::errstr\n"); #Prepare the select query $execu =$sth->execute() or warn $file->log( level = +> 'info', message => localtime() .": Can't execute SQL statement:[$q +uery] $DBI::errstr\n"); #Execute the se +lect query if(!defined($execu)){ $file->log( level => 'info', message => local +time() .": Can't execute SQL statement:[$query] $DBI::errstr\n"); } else{ $file->log( level => 'info', message => local +time() .":SUCCESS:[$query]\n"); } while ( @arra = $sth->fetchrow_array ) { push (@Data_Array,@arra); } $file->log( level => 'info', message => localtime() +.": Fetched Data [@Data_Array]\n"); } else{ $file->log( level => 'info', message => loca +ltime() .": Reconnecting with Database by Ping function\n"); $dbaseh=&connection(); $file->log( level => 'info', message => loca +ltime() .": DataBase Get Connected Successfully.\n" ); $sth=$dbaseh->prepare("$query") or warn $file +->log( level => 'info', message => localtime() .": Can't prepare SQL + statement: $DBI::errstr[$query]\n"); #pre +pare the select query $execu=$sth->execute() or warn $file->log( le +vel => 'info', message => localtime() .": Can't execute SQL statemen +t: $DBI::errstr[$query]\n"); + #execute the select query if(!defined($execu)){ $file->log( level => 'info', message = +> localtime() .": Can't execute SQL statement:[$query] $DBI::errstr\ +n"); } else{ $file->log( level => 'info', message = +> localtime() .":SUCCESS:[$query]\n"); } while ( @arra = $sth->fetchrow_array ) + { push (@Data_Array,@arra); } $file->log( level => 'info', message = +> localtime() .": Fetched Data [@Data_Array]\n"); } $sth->finish(); $dbaseh->disconnect(); + #Disconnect the databa +se. return @Data_Array; }

In reply to DBI : Get Column name and Value in hash by vinoth.ree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-16 05:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found