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

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

Add script to run ingest for all avaiable config files.
Make oai_ingest_new2 a proper object.
Adjust db_funcs - now pass in details to set up database connection

  • although defaults available, if not done.

Simplify coord parsing in PostgresRecord? by using a reusable function.
+ various tidy ups and fixes.

Line 
1#!/usr/bin/env python
2
3# functions for use with NDG discovery postgres db
4
5import pgdb, logging, sys
6
7def db_connect(databaseName='discoverydb', hostName='localhost', userName='postgres', password='pass01word'):
8        '''
9        Open a Postgres database connection
10        @param databaseName: Name of DB to connect to
11        @param hostName: Name of machine where DB is located
12        @param userName: Name of user to connect to DB as
13        @param password: Password for user
14        '''
15        logging.info("Setting up connection to DB: " + databaseName + " on " + hostName)
16        connection_string = hostName + ':' + databaseName + ':' + userName + ':' + password
17        connection = pgdb.connect(connection_string)
18        logging.info("DB connection established")
19        return connection
20
21
22def runSQLCommand(connection, sqlCmd):
23        '''
24        Run a SQL command against a specified DB connection
25        @param connection: a postgres DB connection
26        @param sqlCmd: a SQL command to execute with the postgres connection 
27        '''
28        logging.info("Running SQL command")
29        logging.debug("Actual command: %s" %sqlCmd)
30        cursor = connection.cursor()
31        try:
32            cursor.execute(sqlCmd)
33        except:
34            sys.exit("Error: database error %s %s" %(sys.exc_type, sys.exc_value))
35        connection.commit()
36        logging.info("SQL command completed successfully")
37        return cursor.fetchall()
38
Note: See TracBrowser for help on using the repository browser.