Python Forum

Full Version: Check table exists in Database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can someone help us to work this code

import cx_Oracle


class Table(object):
    def __init__(self, table_name, database):
        self.table_name = table_name
        self.database = database
        self.exists = self.table_exists()

    def table_exists(self):
        conn = get_connection()
        session = conn.cursor()
        cnt = session.execute('SELECT COUNT(*) FROM DBA_OBJECTS WHERE OBJECT_TYPE =:1  AND UPPER(OBJECT_NAME) =:2',
                              ('TABLE', self.table_name))
        return cnt
        session.close()
        conn.close()


def get_connection():
    db_connection = cx_Oracle.connect('XXXXXXX')
    return db_connection


def main():
    c1 = Table
    c1.__init__('MTL_SYSTEM_ITEMS','DEV')

if __name__ == "__main__":
    main()