Connect to Oracle database using SQLALCHEMY

前端 未结 4 958
走了就别回头了
走了就别回头了 2020-12-24 08:24

I am able to successfully connect to a sqlite database and access a particular table using the set of commands below.

from sqlalchemy import create_engine, M         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 09:22

    You don't need to import cx_Oracle anymore. The newer version of the sqlalchemy module calls the function cx_Oracle.makedsn(). Have a look:

    from sqlalchemy.engine import create_engine
    
    DIALECT = 'oracle'
    SQL_DRIVER = 'cx_oracle'
    USERNAME = 'your_username' #enter your username
    PASSWORD = 'your_password' #enter your password
    HOST = 'subdomain.domain.tld' #enter the oracle db host url
    PORT = 1521 # enter the oracle port number
    SERVICE = 'your_oracle_service_name' # enter the oracle db service name
    ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD +'@' + HOST + ':' + str(PORT) + '/?service_name=' + SERVICE
    
    engine = create_engine(ENGINE_PATH_WIN_AUTH)
    
    
    #test query
    import pandas as pd
    test_df = pd.read_sql_query('SELECT * FROM global_name', engine)
    

提交回复
热议问题