{"id":55,"date":"2004-08-03T17:34:31","date_gmt":"2004-08-03T22:34:31","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/perl\/55"},"modified":"2008-01-21T21:35:05","modified_gmt":"2008-01-22T02:35:05","slug":"accessing-sql-in-perl","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Perl\/55\/","title":{"rendered":"Accessing SQL in Perl"},"content":{"rendered":"<p>This is a perl script that uses DBI to open an SQL database, read all the lines of a table, and print them out in an HTML page.  This script assumes you&#8217;ve got a table in your database called people_table, and that you&#8217;ve got at least two fields, one called firstname and one called lastname.<br \/>\nEverything I know about accessing SQP through perl (which isn&#8217;t much) I learned from <a href=\"http:\/\/itp.nyu.edu\/~cs220\/dwd\/class05.html\" target=\"_other\">Chris Sung&#8217;s notes<\/a>, in about half an hour. I copied much of this script from him too.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/networked objects\" rel=\"tag\">networked objects<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/networks\" rel=\"tag\">networks<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><br \/>\nHere&#8217;s the script:<\/p>\n<pre>#!\/usr\/bin\/perl\n\n# Get the directory of this script:\n\nif    ($0=~m#^(.*)\\\\#){ $execDir = \"$1\"; }  # Win\/DOS\nelsif ($0=~m#^(.*)\/# ){ $execDir = \"$1\"; }  # Unix\nelse  {`pwd` =~ \/(.*)\/; $execDir = \"$1\"; }  # Unix\n\n#     get the login (if you get the login like this,\n#    make sure that only you can read and write to the file (chmod 0600 on the file):\nrequire \"$execDir\/info.pl\";\nuse DBI();\n\n#    Set basic parameters:\n$database = $user;\n$data_source = \"dbi:mysql:$database\";\n$username = $user;\n$password = $passwd;\n\n#    Open the database:\n&#38;connectSQL;\n#    Print the top of the HTML page:\n&#38;print_header;\n#    Get everything from the database:\n&#38;get_everything;\n#    Print the bottom of the HTML page:\n&#38;print_footer;\n#    disconnect from the database:\n&#38;disconnectSQL;\n\n#    End the script:\nexit(0);\n\n\n#########################################################\n#    Subroutines\n#########################################################\n\n\n#########################################################\n# Connect to the database.\n#########################################################\n# This only needs to be done once per script\nsub connectSQL {\n     $dbh = DBI-&gt;connect($data_source, $username, $password,\n        {'RaiseError' =&gt; 1, 'PrintError' =&gt; 1});;\n    \n}\n \n \n#########################################################\n# Disconnect from the database.\n#########################################################\n# Always the last thing you do before exiting your script\n\nsub disconnectSQL {\n    $dbh-&gt;disconnect();\n}\n\n#########################################################\n# Get everything from the given table:\n#########################################################\n\n\nsub get_everything {\n    # Retrieve all rows from the table and put in normal array:\n    $SqlStatement = \"SELECT * FROM people_table ORDER BY firstname\";\n    $sth = $dbh-&gt;prepare($SqlStatement);\n    $sth-&gt;execute();\n\n    while (@row_array = $sth-&gt;fetchrow_array())\n    {    $firstname = $row_array[0];\n        $lastname = $row_array[1];\n        print \"$firstname    $lastname &lt;br&gt;\\n\";\n    }\n    $sth-&gt;finish();\n}\n\n#########################################################\n# Print the beginning of an HTML document\n#########################################################\n\nsub print_header {\n#Print the header of the HTML file:    \nprint \"Content-type: text\/html\\n\\n\";\nprint qq^\n&lt;html&gt;\n\n&lt;head&gt;\n    &lt;title&gt;Database Results&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;database results&lt;\/h1&gt;\n^\n}\n\n#########################################################\n# Print the end of an HTML document\n#########################################################\n\nsub print_footer {\nprint qq^\n&lt;\/body&gt;\n\n&lt;\/html&gt;\n^\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a perl script that uses DBI to open an SQL database, read all the lines of a table, and print them out in an HTML page.  This script assumes you&#8217;ve got a table in your database called people_table, and that you&#8217;ve got at least two fields, one called firstname and one called lastname.Everything I know about accessing SQP through perl (which isn&#8217;t much) I learned from Chris Sung&#8217;s notes, in about half an hour.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-Perl"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}