John Delacour wrote:
> I've only been using SQLite for 5 days so I'm very much a beginner. 
> I just spent an hour or so working out how to get a list of column 
> headers from a table and come up with the script below, which will do 
> fine, but I wonder if there's a more elegant way to do it.
> 
> #!/usr/local/bin/perl
> use strict;
> use DBI qw(:sql_types);
> {
> my $db = "a.db";
> my $dbh = DBI->connect("dbi:SQLite:dbname=$db","","") or "...";
> $_ = $dbh->selectall_arrayref("PRAGMA table_info(contacts)") ;
> for (@$_) {push @_, $$_[1]} print join ', ', @_;
> }
> # =>  firm, adr1, postcode1, adr2, postcode2, ...
> 
> JD

How long have you been using Perl?

Anyway, to start with I would replace the last couple lines with:

   my $catalog_rowset = $dbh->selectall_arrayref("PRAGMA table_info(contacts)") 
;
   my @col_names = map { $_->[1] } @{$catalog_rowset};
   print join ', ', @col_names;

Another thing you can try is use DBI's special methods for basic system catalog 
information, rather than using a SQL query to get that information as you did, 
not that the way you did it is wrong per se, but just an alternate means to the 
end.

I refer to http://search.cpan.org/dist/DBI/DBI.pm#Catalog_Methods :

   column_info
   foreign_key_info
   primary_key_info
   table_info
   statistics_info

... and those are described elsewhere on that page.

I haven't used those myself, though, but I believe they are popular for others.

-- Darren Duncan

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to