Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2004
    Posts
    16

    Unanswered: help with column name query

    I am trying to print out column names and it seems i am doing something wrong. Any help??

    use DBI;
    # use DBIx:BSchema::Column;

    #open connection to Access database
    $dbh = DBI->connect('dbi:ODBC:driver=microsoft access driver
    (*.mdb);dbq=C:\Temp\database.mdb');


    # retrieving field names
    $cursor = $dbh->prepare("SELECT * FROM table-name where 1=0");
    $cursor->execute;
    my($fieldNames, $i);
    $fieldNames = $cursor->{"NAME"};
    print "Field Names = ";
    for($i=0; $i < @$fieldNames; $i++)
    {
    print "$fieldNames->[$i], ";
    }
    print "\n";

  2. #2
    Join Date
    Mar 2004
    Posts
    114
    Use fetchrow_hashref method.
    Try this after execute method:
    Code:
    # fetch result in hash ref
    while ( $ref_hash = $sth->fetchrow_hashref ) {
        # something here
    }
    
    # Print the fields
    foreach $key (%$ref_hash) {
             print "$key\n";
    }
    This should work better. The fields name are stored in the keys of the hash referenced.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •