def query_float(self, command, as_dict=False): pg.set_typecast('Numeric', float) pg.set_decimal(float) err = None for i in range(3): try: rs = self.query(command) except ValueError as e: # connection lost, attempt to reconnect self.reopen() err = e else: break else: raise Exception( 'dbConnection.query_float failed after 3 retries. Last error was: ' + str(err)) if as_dict: recordset = rs.dictresult() else: recordset = rs.getresult() pg.set_typecast('Numeric', Decimal) pg.set_decimal(Decimal) return recordset
def __init__(self, configfile, use_float=False): # set casting of numeric to floats pg.set_typecast('Numeric', float) options = { 'hostname': 'localhost', 'username': '******', 'password': '', 'database': 'gnss_data' } self.active_transaction = False self.options = options # parse session config file config = ConfigParser.ConfigParser() config.readfp(open(configfile)) # get the database config for iconfig, val in dict(config.items('postgres')).iteritems(): options[iconfig] = val # open connection to server err = None for i in range(3): try: pg.DB.__init__(self, host=options['hostname'], user=options['username'], passwd=options['password'], dbname=options['database']) # set casting of numeric to floats pg.set_typecast('Numeric', float) if use_float: pg.set_decimal(float) else: pg.set_decimal(Decimal) except pg.InternalError as e: err = e if 'Operation timed out' in str( e) or 'Connection refused' in str(e): continue else: raise e except Exception as e: raise e else: break else: raise dbErrConnect(err) # open a conenction to a cursor self.cursor_conn = pgdb.connect(host=self.options['hostname'], user=self.options['username'], password=self.options['password'], database=self.options['database']) self.cursor = self.cursor_conn.cursor()
def __init__(self, configfile, use_float=False): # set casting of numeric to floats pg.set_typecast('Numeric', float) options = {'hostname': DB_HOST, 'username': DB_USER, 'password': DB_PASS, 'database': DB_NAME} self.active_transaction = False self.options = options # parse session config file config = configparser.ConfigParser() config.read_string(file_read_all(configfile)) # get the database config options.update(dict(config.items('postgres'))) # open connection to server err = None for i in range(3): try: pg.DB.__init__(self, host = options['hostname'], user = options['username'], passwd = options['password'], dbname = options['database']) # set casting of numeric to floats pg.set_typecast('Numeric', float) pg.set_decimal(float if use_float else Decimal) except pg.InternalError as e: err = e if 'Operation timed out' in str(e) or \ 'Connection refused' in str(e): continue else: raise e else: break else: raise dbErrConnect(err) # open a conenction to a cursor self.cursor_conn = pgdb.connect(host = options['hostname'], user = options['username'], password = options['password'], database = options['database']) self.cursor = self.cursor_conn.cursor()
def __init__(self, configfile, use_float=False): # set casting of numeric to floats pg.set_typecast('Numeric', float) options = { 'hostname': 'localhost', 'username': '******', 'password': '', 'database': 'gnss_data' } self.active_transaction = False self.options = options # parse session config file config = ConfigParser.ConfigParser() config.readfp(open(configfile)) # get the database config for iconfig, val in dict(config.items('postgres')).iteritems(): options[iconfig] = val # open connection to server tries = 0 while True: try: pg.DB.__init__(self, host=options['hostname'], user=options['username'], passwd=options['password'], dbname=options['database']) # set casting of numeric to floats pg.set_typecast('Numeric', float) if use_float: pg.set_decimal(float) else: pg.set_decimal(Decimal) break except pg.InternalError as e: if 'Operation timed out' in str( e) or 'Connection refused' in str(e): if tries < 4: tries += 1 continue else: raise dbErrConnect(e) else: raise e except Exception as e: raise e
def query_float(self, command, as_dict=False): pg.set_typecast('Numeric', float) pg.set_decimal(float) rs = self.query(command) if as_dict: recordset = rs.dictresult() else: recordset = rs.getresult() pg.set_typecast('Numeric', Decimal) pg.set_decimal(Decimal) return recordset