Skip to content. | Skip to navigation

Sections
Personal tools
Navigation
Log in


Forgot your password?
 

Templates for DB connections

Frequently used pieces of code.

The ODBC connection needs to be accessible to R for this to work. You need to create a DNS entry to the database of your choice. In this example, the DNS entry is called "gfsDB".

#R code to connect to postGIS server on nautilus.

require(RODBC)
chan <- odbcConnect(dsn="gfsDB", uid = "postgres",
              case='postgresql',believeNRows=FALSE)

qu <- paste("SQL QUERY GOES HERE ")

mydata <- sqlQuery(chan, qu, errors= TRUE)
odbcClose(chan)

Doing the same in Perl, using native postgreSQL bindings here, not ODBC:

#!/usr/bin/perl
use DBI qw(:sql_types);

$dbh = DBI->connect(
        'dbi:Pg:dbname=gfsDB;host=localhost;port=5432',
                        'postgres',
                        '',
                        {
                          RaiseError => 1,
                          AutoCommit => 0
                        }
                      ) || die "Database connection not made: $DBI::errstr";


my $sql = qq{SQL QUERY GOES HERE };

my $sth = $dbh->prepare( $sql );
$sth->execute();

while( $sth->fetch() ) {DO YOUR THING WITH THE INCOMING DATA}

$sth->finish();
$dbh->disconnect();


Document Actions

« February 2017 »
February
MoTuWeThFrSaSu
12345
6789101112
13141516171819
20212223242526
2728