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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |