Python Forum
Check table exists in Database - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Check table exists in Database (/thread-21532.html)



Check table exists in Database - ARV - Oct-03-2019

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()