Chemistry Reference
In-Depth Information
functions to fetch results from an SQL statement. These are not consid-
ered here further, but are described elsewhere. 10,11
5.5.2 Python
Python is a general-purpose computer language with no built-in capabili-
ties to access an RDBMS. There are modules that extend python to allow
interaction with an RDBMS, for example, pygresql 12 for PostgreSQL or cx_
oracle for Oracle. Once pygresql is installed, the following python script
will fetch some rows and columns from a PostgreSQL database.
import pg
conn = pg.connect(dbname='book', host='rigel', user='reader')
sql = "Select smiles,cas from nci.structure where
matches(smiles,'c1ccccc1 C(=O)NC')"
for (smi,cas) in (conn.query(sql).getresult()):
print smi, cas
conn.close()
The connect function opens the connection to the RDBMS using the
appropriate database name, host name, username, and password. The
query and getresult methods execute the SQL statement and get the
results. There are other methods available to get results, but these are dis-
cussed elsewhere. 13
5.5.3 PHP
PHP is a general-purpose computer language often used as a CGI program
serving as part of a Web application. The interface to an RDBMS is compiled
into PHP. When PHP is installed, the appropriate version must be selected
to allow interaction with the necessary RDBMS. Of course, it is possible to
compile PHP from source, specifying the options to include RDBMS sup-
port. PHP supports interaction with many RDBMS, including Oracle 14 and
PostgreSQL. 15 Once the proper version of PHP is installed, the following
php script will fetch some rows and columns from a PostgreSQL database.
<?php
$dbconn = pg_connect("host=rigel dbname=book
user=reader password=something");
$sql = "Select smiles,cas from nci.structure where
matches(smiles,'c1ccccc1C(=O)NC')";
$result = pg_query($dbconn, $sql);
while ( $row = pg_fetch_array($result) ) {
print $row['smiles'] . "\t" . $row['cas'] . "\n";
}
pg_close($dbconn);
?>
Search WWH ::




Custom Search