Search notes:

Perl module GraphViz::DBI

#!/usr/bin/perl
use warnings;
use strict;

use DBI;
use GraphViz::DBI;

my $database_name = 'the.db';

unlink $database_name if -e $database_name;

create_database();
create_erd();

sub create_database {

  my $dbh = open_db();

  $dbh->do(q{
    create table T1 (
      col_1   number,
      col_2   varchar(10),
      col_3   date,
--    col_4   number not null,
      primary key (col_1)
    )
  });

  $dbh->do(q{
    create table T2 (
      col_a   number,
      col_b   varchar(10),
      col_c   varchar(10),
      col_d   varchar(20),
      primary key (col_b, col_c),
      foreign key (col_a) references T1
    )
  });

  $dbh->do(q{
    create table T3 (
      col_x   number,
      col_y   number,
      col_z   varchar(99),
      primary key (col_x)
    )
  });

  $dbh->do(q{
    alter table T1 add col_4 number references T3
  });

  $dbh -> disconnect;

}

sub create_erd {

  my $dbh = open_db();

# $dbh -> do('PRAGMA foreign_keys = ON') if ($ENV{DBI_DSN} =~ /SQLite/i);

  open my $erd_fh, '>', 'erd.png' or die;
  print $erd_fh GraphViz::DBI->new($dbh)->graph_tables->as_png;
  close $erd_fh;

  $dbh->disconnect;

}

sub open_db {

  my $dbh = DBI->connect("dbi:SQLite:dbname=$database_name") or die "Could not create $database_name";

  return $dbh;


}
Github repository PerlModules, path: /GraphViz/DBI/script.pl
Unfortunately, DBI seems not able to collect the foreign/primary key relationships of an sqlite database.

See also

GraphViz::DBI
Perl modules

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1741108750, '3.136.236.173', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/modules/GraphViz/DBI/index(121): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78