source: cows_wps/trunk/cows_wps/utils/db_connection.py @ 5615

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/utils/db_connection.py@7575
Revision 5615, 904 bytes checked in by spascoe, 10 years ago (diff)

COWS WPS package copied from
 http://proj.badc.rl.ac.uk/svn/dcip/cows-wps/trunk.

This is a stripped down version of the DDP WPS. Some features are
removed and others have been deactivated until we reimplement them in a
more generic way.

Line 
1"""
2db_connection.py
3================
4
5"""
6
7
8import pgdb
9
10
11class DBConnection:
12
13    def __init__(self, **kwargs):
14        pw = self._getPW()
15        if kwargs.has_key("connect_string"):
16            self.db = pgdb.connect(kwargs["connect_string"] + ":" + pw)
17        else:
18            kwargs["password"] = pw
19            self.db = apply(pgdb.connect, [], kwargs)
20
21    def _getPW(self):
22        return open("/disks/westerly2/.pgpw").read().strip()
23
24    def query(self, query_string):
25        self.c = self.db.cursor()
26        self.c.execute(query_string)
27        return self.c.fetchall()
28
29
30if __name__ == "__main__":
31
32    db = DBConnection(connect_string='localhost:ukcp09_7:dalderson')
33    print db.query("SELECT * FROM userdb WHERE username='astephen'")
34
35    db2 = DBConnection(host="localhost", database="ukcp09_7", user="dalderson")
36    print db.query("SELECT * FROM userdb WHERE username='astephen'")
Note: See TracBrowser for help on using the repository browser.