This is concise, but don't do it this way — I was just challenging myself to output the entire table in one print statement using no pesky variables. Fetching one row at a time is more memory efficient if your table has more than a few rows.

use strict; use warnings; use DBI; use Data::Dumper; use HTML::Entities qw/encode_entities/; my $dbh = DBI->connect('DBI:SQLite:', undef, undef, { RaiseError => !! +1 }); $dbh->do($_) for split /;;/, <<'GO'; CREATE TABLE example ( id integer, name varchar, age integer, PRIMARY KEY(id) );; INSERT INTO example VALUES (1, 'Alice', 25);; INSERT INTO example VALUES (2, 'Bob', 26);; INSERT INTO example VALUES (3, 'Carol', 24);; INSERT INTO example VALUES (4, 'Dave', 25);; INSERT INTO example VALUES (5, 'Eve', 666);; GO my $sth = $dbh->prepare('SELECT * FROM example'); $sth->execute(); print( "<table>\n". "\t<thead>\n". "\t\t<tr>".join('', map sprintf('<th>%s</th>', encode_entities($_) +), @{$sth->{NAME_lc}})."</tr>\n". "\t</thead>\n". "\t<tbody>\n". join("\n", map sprintf( "\t\t<tr>%s</tr>", join '', map sprintf('<td>%s</td>', encode_entities($_)), +@$_ ), @{ $sth->fetchall_arrayref })."\n". "\t</tbody>\n". "</table>\n" );

In reply to Re: How to copy data pulled from a sql query into a HTML Table by tobyink
in thread How to copy data pulled from a sql query into a HTML Table by harshay7

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.