Connect to Oracle database using SQLALCHEMY

前端 未结 4 963
走了就别回头了
走了就别回头了 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:14

    from sqlalchemy import create_engine
    import cx_Oracle
    
    host=hostname
    port=port
    sid='sid'
    user='username'
    password='password'
    sid = cx_Oracle.makedsn(host, port, sid=sid)
    
    cstr = 'oracle://{user}:{password}@{sid}'.format(
        user=user,
        password=password,
        sid=sid
    )
    
    engine =  create_engine(
        cstr,
        convert_unicode=False,
        pool_recycle=10,
        pool_size=50,
        echo=True
    )
    
    result = engine.execute('select * from TABLE')
    
    for row in result:
        print row
    

    This worked for me. A connection object can also be created like

    conn = engine.connect()
    conn.close()
    

    which will enable to close the connection. This works even if you have a tunnel to your remote DB from your local port.

提交回复
热议问题