source: TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/db_funcs.py @ 3817

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/db_funcs.py@3817
Revision 3817, 1.2 KB checked in by cbyrom, 11 years ago (diff)

Add default logging support + create new version of ingest script, removing
all traces of the eXist DB + improve documentation and output.

Line 
1#!/usr/bin/env python
2
3# functions for use with NDG discovery postgres db
4
5import pgdb, logging
6
7def db_connect():
8        '''
9        Open a Postgres database connection
10        '''
11        DATABASE = 'test'
12        HOST     = 'localhost'
13        USER     = 'postgres'
14        PW       = 'pass01word'
15        SCHEMA   = 'public'
16        #       DATABASE = 'xxxx'
17        #       HOST     = 'xxx.xxx.uk'
18        #       USER     = 'xxx'
19        #       PW       = 'xxxxxxx'
20        #       SCHEMA   = 'xxx'
21        logging.info("Setting up connection to DB: " + DATABASE + " on " + HOST)
22        connection_string = HOST + ':' + DATABASE + ':' + USER + ':' + PW
23        connection = pgdb.connect(connection_string)
24        logging.info("DB connection established")
25        return connection
26
27
28def runSQLCommand(connection, sqlCmd):
29        '''
30        Run a SQL command against a specified DB connection
31        @param connection: a postgres DB connection
32        @param sqlCmd: a SQL command to execute with the postgres connection 
33        '''
34        logging.info("Running SQL command")
35        logging.debug("Actual command: %s" %sqlCmd)
36        cursor = connection.cursor()
37        try:
38            cursor.execute(sqlCmd)
39        except:
40            sys.exit("Error: database error %s %s" %(sys.exc_type, sys.exc_value))
41        connection.commit()
42        logging.info("SQL command completed successfully")
43        return cursor.fetchall()
44
Note: See TracBrowser for help on using the repository browser.