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
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,113 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to check spec table in another df Mekala 0 1,388 Oct-11-2020, 02:25 PM
Last Post: Mekala
  Check if a file exists. Pedroski55 5 3,251 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,140 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 2,301 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 2,763 Dec-26-2019, 05:57 AM
Last Post: buran
  How read Table Structure from Database nnsatpute 5 3,659 Oct-25-2019, 10:08 AM
Last Post: Larz60+
  Database table structure compare(Oracle) GKT 4 2,671 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 20,004 May-27-2018, 05:44 AM
Last Post: buran
  Python code to check SQL table for current date PYTHONDUDE 3 2,885 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