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

The boss wants a HTML page to run query to SQL server. So I am trying to figure out how to accomplish this. Thought Perl could be my answer.

So I guess the question is How do I get the HTML text fields to complete a query, run the query, SQL I think, and display results on the same HTML I started with?
I don't have code at this time. Trying to figure out where to start. I am not asking you guys to write my code.
Just needing direction on how and what I need to learn. Maybe and explanation of the processes.
Thanks for your time and thoughts.

Replies are listed 'Best First'.
Re: Need GUI for easy query and results
by Corion (Patriarch) on Mar 10, 2017 at 20:59 UTC
Re: Need GUI for easy query and results
by thanos1983 (Parson) on Mar 10, 2017 at 20:57 UTC
Re: Need GUI for easy query and results
by huck (Prior) on Mar 10, 2017 at 21:28 UTC

    How do I get the HTML text fields to complete a query

    • how much HTML do you know?
    • can you create a HTML <form> with <input> fields?
    • as far as i know you will need a web server to process a <form> do you have a web server and do you have permissions to write to the cgi-bin libraries?
    To write and process forms. I still use CGI but that is being depreciated and some may suggest CGI::Simple to write and process forms.

    run the query, SQL I think

    I mostly use DBD::SQLite a subset of DBI, but at times use DBD::mysql.

    All the DBI routines share the same query model. first you connect

    my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
    (above xample from DBD::SQLite)

    then you setup.
    $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
    then you run
    $sth->execute( $baz ); while ( @row = $sth->fetchrow_array ) { print "@row\n"; }
    (above example from DBI)

    display results on the same HTML I started with?

    Well that isnt quite true, you get some HTML, fill in the form, press the submit button, then you get NEW HTML back. But it will probably be from the same address. For the most part the same cgi code will first construct the page with the empty form, then when you press submit get the input from your browser, execute your sql from the input fields, then construct a page with the results and another form.

Re: Need GUI for easy query and results
by shmem (Chancellor) on Mar 11, 2017 at 11:44 UTC

    I don't know how much experience you have in perl, web and database, so here's the basics - the requisites and a simple script to get you started. You need:

    • a configured web server which allows CGI scripts
    • a database server
    • the modules DBI and CGI

    The html page comprises a form with a method, and some inputs. The browser sends the input names along with their values to the webser server, which parses the request and provides the name/value tupels to the script, depending on the request method:

    • GET = via the environment variable QUERY_STRING (see %ENV)
    • POST = via the standard input file descriptor STDIN

    You take these request values and query the database with them, then you send the results back.

    So, you could roll your own parsing of the query like so:

    my $query; if ($ENV{REQUEST_METHOD} eq 'GET') { $query = $ENV{QUERY_STRING}; } elsif ($ENV{REQUEST_METHOD} eq 'POST') { read STDIN, $query, $ENV{CONTENT_LENGTH},0; } # make a hash from the query my %param; my @tupels = split /(\&|;)/,$query; for my $tupel(@tupels) { my ($key,$value) = split /=/, $tupel; $param{$key} = $value; }

    but this is highly discouraged since this basic code, provided as an example only doesn't handle all edge cases (e.g. multivalue parameters) and could make your script vulnerable. Better (not best, though) is using the CGI module.

    The following script provides a simple form, and the results of the query, if valid POST data has been sent along with the request.

    You need a table persons in the test database on your SQL server, like so:

    CREATE TABLE persons ( name varchar(255), age tinyint );

    and of course, fill it with some values.

    Change the username and password below to reflect your setup.

    #!/usr/bin/perl -T # always use -T for CGI stuff. read perlsec use strict; use warnings; use CGI qw(:standard); # this pulls in param(), header() and start_htm +l() use DBI; my @query_results; my $error; my $name; # if we have a 'name' param, check it... if (param('name')) { if (param('name') =~ /^(\w+)$/) { $name = $1; } # ...and if name param is ok query the DB if ($name) { my $dbh = DBI->connect( "DBI:mysql:database=test", 'testuser', # username - change to your setup 'secretpw', # password - change to your setup {RaiseError => 1}, ) or $error = "could not connect to database"; my $sth = $dbh->prepare( "select name, age from persons where name = ?" ); $sth->execute($name); while(my $row = $sth->fetchrow_arrayref) { push @query_results, $row; } } } else { $error = "please enter a valid name\n"; } print header(), start_html('SQL Query'); print <<EOH; <html> <head><title>SQL</title></head> <body> <h1>SQL Query</h1> <form method="POST" action="/cgi-bin/sql.cgi"> <input id="name" name="name"/> </form> <hr> <table border=1> <thead><tr><td>Name</td><td>Age</td></tr></thead> <tbody> EOH foreach my $row (@query_results){ my ($name, $age) = @$row; print "<tr><td>$name</td><td>$age</td></tr>\n"; } print "</tbody>\n</table>\n"; print "<b>$error</b>" if $error; print "</body></html>\n";

    See perlsyn, perldata, perlvar, perlsec; read perfunc, perlref, perlreftut ...well, read the complete perl documentation ;-). Read the CGI and DBI manual pages.

    There are complete web frameworks to be found on the web for CRUD operations (create/read/update/delete) which you should consider if your project comprises more than simple queries, and as your familiarity with perl grows.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'