Python Forum
Check table exists in Database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check table exists in Database
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to check spec table in another df Mekala 0 171 Oct-11-2020, 02:25 PM
Last Post: Mekala
  Check if a file exists. Pedroski55 5 635 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 593 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 380 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 457 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  MYSQL how to assign a table for each user in my username table database? YoshikageKira 1 929 Dec-26-2019, 05:57 AM
Last Post: buran
  How read Table Structure from Database nnsatpute 5 1,361 Oct-25-2019, 10:08 AM
Last Post: Larz60+
  Database table structure compare(Oracle) GKT 4 779 Oct-16-2019, 03:16 PM
Last Post: GKT
  How to check if class instance exists in a list of class instance objects? sonicblind 23 8,370 May-27-2018, 05:44 AM
Last Post: buran
  Python code to check SQL table for current date PYTHONDUDE 3 1,149 May-16-2018, 02:27 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020