

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang=en>
<head>
<title>Web Conference 2004 - Perl/CGI - SQLite</title>
<meta http-equiv="Content-Type" content="text/html">
</head>
<body bgcolor="#ffffff">
<table width="100%"><tr valign="top"><td width="144">
<a href="http://www.psu.edu/">
<img src="psu.gif" width="141" height="83" border="0" alt="Penn State"></a></td>
<td>
  <center><h1>Web Conference 2004</h1></center>
  <center><h3>Writing Perl/CGI Scripts for ITS/ASET Web services</h3></center>
  <center><h4>SQLite</h4></center>
</td>
<td width="141"></td></tr></table>








<table border=0 width="100%" align=center><tr>

<td align=right width="49%"><a href="datafiles.html">&lt;- Back - Data files</a></td><td>|</td>

<td><a href="./">Up</a></td>

<td>|</td><td width="49%"><a href="webauth.html">Web Access Control - Next -&gt;</a></td>

</tr></table>


<h3>Databases and dynamic Web content</h3>


<p>See also: <a href="../2005/sqlite.php">Lesson on using SQLite from PHP</a>.</p>

<ul>
<li><p>Databases, from spreadsheets (like Excell) to Access and beyond - storing atoms of data in tables, creating relationships and querying data</p>
<li><p>Relational Database Management Systems (RDBMS)</p>
<ul><li><p>Examples: MySQL, PostgreSQL, SQL Server, Oracle, DB2</p></ul>
<li><p>Structured Query Language (SQL)</p>
<ul><li><p>select &lt;items&gt; from &lt;tables&gt; [with some conditions]</p>
    <li><p>insert into &lt;table&gt; values &lt;items...&gt;</p></ul>
<li><p>SQL92 - a standard that no one fully supports, but the common ground from which each is measured</p>
<li><p>Programmatic access to databases - include Open DataBase Conntectivity (ODBC), Java DataBase Connectivity (JDBC), Perl DBI and DBDs, 
</ul>

<h3>Perl and databases</h3>

<p>Like many other programming languages, Perl has a means to access
databases.  The <b>Perl DataBase Interface (DBI)</b> is a module of
Perl that can access just about any database based on SQL.  The DBI
shields the programmer from the details of the specific drivers,
<b>Perl DataBase Driver (DBD)</b> modules, that need to be loaded for
each DBMS.</p>

<table border=1><tr><td><pre>
#!/usr/local/bin/perl

use DBI;

$database_handle = DBI-&gt;connect( "dbi:mysql:database=FinanceDB;host=192.168.46.59;port=3306", "joe", "fel1X" );

$query = $database_handle -&gt; prepare("select * from accounts_payable where credit &gt; 20000");

$query -&gt; execute();

print "Query status: " . $database_handle -&gt; state . "\n";

while ( @rows = $query-&gt;fetchrow_array() ){
	print "[ ";
	foreach $item (@rows){
		print "{ $item }";
	}
	print " ]\n";
}
$query-&gt;finish();

$database_handle-&gt;disconnect();

</pre></td></tr></table>

<h3>SQLite - a simple file-based database</h3>

<p>SQLite is a database system that stores all of its data into a
single file.  There is no server or running process to connect to like
other RDBMSs, and all of the program code to handle reading and writing
the file is stored in the <i>library</i> ("<i>module</i>") or program
code portion.  It implements SQL92, so has relational abilities like
mainstream RDBMSs, however there is no security other than what the
filesystem grants to the database file.  It also doesn't scale beyond
the capabilities of access to a single file, but is useful for learning
Perl DBI without the work needed to access a regular RDBMS.</p>

<p>SQLite is installed in PASS and usable in Perl CGI scripts from the
ITS/ASET Web servers, such as test.scripts.psu.edu.</p>

<p><a
href="http://test.scripts.psu.edu/users/j/c/jcd/useful/webcon/2004/sql_interface.cgi">
sql_interface.cgi</a> - a Perl CGI script that uses SQLite.  You can
install your own copy by following these instructions:</p>

<ol>

<li><p>Go to the <a
href="http://test.scripts.psu.edu/users/j/c/jcd/useful/webcon/2004/sql_interface.cgi">
sql_interface.cgi</a> page, and download the source code for "this
script" (sql_interface.cgi) and the "internal script"
(sql_internal.perl).  Use the right mouse button to "Save Target As..." to download as you did for helloworld.cgi.</p>

<li><p>Open <b>sql_internal.perl</b> in WordPad as you did helloworld.cgi.</p>

<li><p>Change the line:</p>
<pre>my $db_file = '/.../dce.psu.edu/fs/users/j/c/jcd/www/useful/webcon/2004/db/database_file';</pre>
<p>To be the path to your database file (which will be created).  Such as:</p>
<pre>my $db_file = '/.../dce.psu.edu/fs/users/x/y/xyz123/www/scripts/db/database_file';</pre>

<li><p>Edit <b>sql_interface.cgi</b> in WordPad as you did
helloworld.cgi and change the HTML to suit your tastes.  This step is
optional.</p>

<li><p>Upload both files to your <b>scripts</b> folder and make sure
you saved them as Text Documents and uploaded them in ASCII mode as you
did other CGI script files.</p>

<li><p>While still in SSH Secure File Transfer, create a sub-folder in
your <b>scripts</b> folder and name it "<b>db</b>".  You can create a "New
Folder (Ctrl+N)" via the "Operation" menu.</p>

<li><p>Go to ACL Explorer (via <a href="https://www.work.psu.edu/"
target=_new>https://www.work.psu.edu/</a>) and set <b>R</b>ead and
e<b>X</b>ecute permissions for everyone on both sql_interface.cgi and
sql_internal.perl as you did for other CGI scripts.</p>

<li><p>While still in ACL Explorer, change the permissions on the
folder <b>db</b>.  Set the permissions for the group test.scripts.psu.edu to
have <b>R</b>ead, <b>W</b>rite, e<b>X</b>ecute, <b>I</b>nsert and
<b>D</b>elete.</p>


</ol>

<table border=0 width="100%" align=center><tr>

<td align=right width="49%"><a href="datafiles.html">&lt;- Back - Data files</a></td><td>|</td>

<td><a href="./">Up</a></td>

<td>|</td><td width="49%"><a href="webauth.html">Web Access Control - Next -&gt;</a></td>

</tr></table>

<hr>
<p><font size="-1">
If you have any questions, feel free to ask me - mailto:j&#x63;d&#x40;p&#x73;u&#x2e;e&#x64;u
</font></p>
<p><font size="-1">
Content by: Jeff D'Angelo &lt;j&#x63;d&#x40;p&#x73;u&#x2e;e&#x64;u&gt; &copy; 2004
</font></p>
<p><font size="-1">
Last update on: Friday, 27-Apr-2007 10:20:11 EDT 
</font></p>

</body>
</html>

