def _get_connection(connection): if isinstance(connection, (six.binary_type, six.text_type)): return pgconnect(connection) else: # already a connection return connection
def InsertDB(self, context, data): db_con = self.postgres_con table_name = self.postgres_table_name dbcon = pgconnect(db_con) # check if database table exists curs = dbcon.cursor() curs.execute( "select * from information_schema.tables\ where table_name=%s", (table_name,), ) if not curs.rowcount: self.create_pgtable(data, dbcon) curs.close() curs = dbcon.cursor() sqlstmt, sqldata = self.generate_insert_stmt(data) try: curs.execute(sqlstmt, sqldata) dbcon.commit() return except Exception as e: logger.info( u'----------- insert statement tried: \n {}\n\{}\n\n -----\n'.format( sqlstmt, sqldata ) ) raise e finally: curs.close() dbcon.close()
def connect(self): return pgconnect( dbname=self.cnf.postgres_db, user=self.cnf.postgres_user, password=self.cnf.postgres_password, host=self.cnf.postgres_host, port=self.cnf.postgres_port, options='-c search_path=' + self.cnf.postgres_schema, )
def get_pgconnection(): pgconnection = pgconnect(dbname='opentaps_seas') register_hstore(pgconnection) return pgconnection