Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a real newbie to perl and SQLite. I have several sqlite databases, but have no idea of their tables / table structures. Is there a way with dbd:SQLite to print a list of tables and then to describe the tables so I have a refference schema from which to develop my applications? I am using activestate perl 5.10.1.1007 on a windows xp pro sp3 system. Any tips on using dbd:SQLite would also be greatly appreciated.
  • Comment on determining table structure in SQLite database

Replies are listed 'Best First'.
Re: determining table structure in SQLite database
by Corion (Patriarch) on Jul 28, 2010 at 21:03 UTC

    DBI has various Catalog Methods. See the column_info and/or table_info methods of the database handle, which return a resultset from which you can fetch the information.

Re: determining table structure in SQLite database
by FunkyMonk (Bishop) on Jul 28, 2010 at 23:37 UTC
    As well as using DBD::SQLite via DBI's methods, you can use the sqlite3 command line tool:
    sqlite3 <your sqlite database> .schema

    Just substitute the file you've used for the database.

Re: determining table structure in SQLite database
by ww (Archbishop) on Jul 28, 2010 at 21:27 UTC
    Just for the record, you can also get the schema info direct, from the command line, using only the contents of the SQLite package... which, IIRC, has within its documentation, examples of how to do this.
Re: determining table structure in SQLite database
by NetWallah (Canon) on Jul 29, 2010 at 06:36 UTC
    I wrote this code recently - it reads the schema, and attempts to guess linkages based on field and table names.

    Ignore/delete the CGI /print code.

    #------------------------------------------------------ sub Show_Tables_and_Identify_Linkages{ my ($tableRef) = @_; #Tableref gets populated here #--- Show View and Table names/links ------- my %Do_not_render = map {$_ => 1} qw|XP_PROC sqlite_sequence|; print start_table({-border=>undef, -width=>'75%', -align=>'CENTER' +,-cellspacing=>0, -cellpadding=>2, -bgcolor=>'SKYBLUE'}), "\n", start_Tr; # -- Use DBI "table_info" to get Table/view, then dig and get Colu +mn names -- # Store obtained info into $tableref, and display table/view names + in html my $count; my %stylemap = (table=>'SampleBlock ShowTable', view=>'SampleBlock + ShowView'); my $sth = $dbh->table_info(undef,undef,'%',"'TABLE','VIEW'" );#( +$catalog, $schema, $table, $type )); while (my $t = $sth->fetchrow_hashref() ){ next if $Do_not_render{ $t->{TABLE_NAME} }; $count++ % 4 == 0 and print end_Tr,"\n",start_Tr; param("NamedQuery", escape($t->{TABLE_NAME})); print td( {-class=>$stylemap{ lc($t->{TABLE_TYPE}) } }, a({-href=>url(-relative=>1,-query=>1), -class=>'ShowView' +, -title=> $t->{TABLE_TYPE} . ' query'}, $t->{TABLE_NAME})); my @primary_keys = $dbh->primary_key( @{$t}{qw|TABLE_CAT TABLE +_SCHEM TABLE_NAME|});# $catalog, $schema, $table ); s/"//g for @primary_keys; # Zap quotes $tableRef->{ $t->{TABLE_NAME}}{_TYPE}= $t->{TABLE_TYPE}; $tableRef->{ $t->{TABLE_NAME}}{_TABLELC}= lc $t->{TABLE_NAME}; my $colcount=0; my $colinfo = $dbh->column_info(@{$t}{qw|TABLE_CAT TABLE_SCHEM + TABLE_NAME|},'%' );#$catalog, $schema, $table, $column ); while (my $c = $colinfo->fetchrow_hashref() ){ push @{ $tableRef->{$t->{TABLE_NAME}}{FIELDS} },$c->{CO +LUMN_NAME}; push @{ $tableRef->{$t->{TABLE_NAME}}{FIELDSLC} }, lc($c- +>{COLUMN_NAME}); $tableRef->{$t->{TABLE_NAME}}{ $c->{COLUMN_NAME} }{TYPE}= +$c->{TYPE_NAME};# INTEGER TEXT etc $tableRef->{$t->{TABLE_NAME}}{ $c->{COLUMN_NAME} }{PK} = +grep {m/$c->{COLUMN_NAME}/} @primary_keys; # Primary Key } } $sth->finish; print "\n",end_Tr, end_table,"\n"; CGI::delete(qw|q NamedQuery|); # From parameter list - keep this c +lean # Identify Table Linkages (Must be post-processed after ALL table/ +field names are obtained) ----- # A Field named "xxxid' is assumed to be an FKEY to table xxx for my $t (keys %$tableRef){ for my $f (@{ $tableRef->{$t} {FIELDS} } ){ next if $tableRef->{$t}{$f}{PK}; # Cannot link Primary keys next unless my ($otherTable) = grep {$_ ne $t && $tableRef->{$ +_}{_TABLELC} . "id" eq lc($f) } keys %table; $tableRef->{$t}{$f}{LINKEDTABLE} = $otherTable; for my $fk( @{$tableRef->{$otherTable}{FIELDS}} ){ next unless $tableRef->{$otherTable}{$fk}{PK} or lc( $fk ) eq "id"; $tableRef->{$t}{$f}{LINKEDFIELD} = $fk; push @{ $tableRef->{$otherTable}{LINKEDTABLES} }, [$t,$f, +$fk ]; last; } } } }

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: determining table structure in SQLite database
by Anonymous Monk on Jul 29, 2010 at 03:01 UTC