Redshift SQL Helper

Posted on February 11, 2020

When analyzing data from a database, usually redshift for me, it can be an absolute pain switching from your database IDE to your python IDE. To avoid this, I created a module that allows one connect to Redshift and execute a group of queries from a text file. I have found this immensely helpful and hope you do too!

import psycopg2
from connection_config import cc

class RedshiftSQLHelper:

    def __init__(self, fin):
        self.fin = fin
        self.sql = open(fin, 'r').read()
        self.conn = psycopg2.connect(**cc)
        self.cur = self.conn.cursor()
        self.commands = list()
        self.split_commands()

    def split_commands(self):
        sql = open(self.fin, 'r').read()
        for command in sql.split(';\n'):
            command = command.strip()
            if command != '':
                self.commands.append(command)

    def print_commands(self):
        for command in self.commands:
            print('\n' + '*' * 80 + '\n\n', command)

    def excecute_commands(self):
        for command in self.commands:
            print('\n' + '*' * 80 + '\n\n', 'Executing:', '\n', command)
            self.cur.execute(command)
        self.cur.close()
        print('\n' + '*' * 80 + '\n\n', 'Execution Complete')