I once did something like this. Below is a sample that creates a simple graph with nodes and edges showing relationships between them:
#!/usr/bin/perl use strict; use warnings; use DBI; use GraphViz; use Data::Dumper; use Getopt::Long; use Log::Log4perl; use Pod::Usage; my $conf = undef; my $log = undef; my $help = undef; my $db = undef; my $dbuser = undef; my $dbpass = undef; $conf = q( log4perl.category.Script = DEBUG, ScreenAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.stderr = 1 log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %F +:%L - %m%n ); Log::Log4perl::init( \$conf ); $log = Log::Log4perl::->get_logger(q(Script)); $log->debug("start") if $log->is_debug(); # GetOptions GetOptions("help|?" => \$help, "db=s" => \$db, "dbuser=s" => \$dbuser, "dbpass=s" => \$dbpass) or pod2usage(2); pod2usage(1) if $help; pod2usage(1) if !$db; pod2usage(1) if !$dbuser; pod2usage(1) if !$dbpass; my $dbh = DBI->connect ('DBI:mysql:' . $db, $dbuser, $dbpass); my $sth = $dbh->prepare( q{select name, friend from friends} ); $sth->execute() or $log->logdie($dbh->errstr); my %friends = (); while (my $hashref = $sth->fetchrow_hashref()) { my ($name, $friend) = ($hashref->{name}, $hashref->{friend}); $log->debug(qq{$name, $friend}) if $log->is_debug(); push @{$friends{$hashref->{name}}}, $friend; } $sth->finish(); $log->debug(Dumper(\%friends)) if $log->is_debug(); my $g = GraphViz->new(layout => 'fdp', ratio => 'compress'); # 1st iteration : Add nodes foreach my $name (keys %friends) { $g->add_node( "$name", label => $name, shape => 'box', style => 'filled', ) or $log->logdie("add_node failed"); } # 2nd iteration : Add edges foreach my $name (keys %friends) { foreach my $friend ( @{$friends{$name}}) { $log->debug(qq{$name => $friend}) if $log->is_debug(); $g->add_edge("$name" => "$friend", label => q{loves}) or $log->logdie("add_edge failed"); } } # Write to file my $fh = undef; open($fh, "> friends.png") or die "open failed:$!"; print $fh $g->as_png; close $fh or die "close failed:$!"; __END__ =head1 NAME simpsons - Graphviz DBI example =head1 SYNOPSIS ./simpsons.pl --db DB --dbuser DBUSER --dbpass DBPASS =head1 MySQL DDL, DML create table friends (name varchar(32) not null, friend varchar(32) no +t null); alter table friends add unique key (name, friend); insert into friends values ('Homer', 'Marge'), ('Homer', 'Bart'), ('Homer', 'Lisa'),('Marge', 'Homer'),('Bart', 'Santa\'s Little Helper'),('Lisa', 'Snowball II'); =cut
--
Andreas

In reply to Re: how to use Graphviz with sql by andreas1234567
in thread how to use Graphviz with sql by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.