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, MetaData, Table, and_
from sqlalchemy.sql import select
from pandas import DataFrame
db = create_engine('sqlite:///path\\database.db')
metadata = MetaData(db)
table = Table('table name', metadata, autoload=True)
I am able to fetch data from an Oracle database using cx_Oracle.
However, when I try to connect to an Oracle database in SQLAlchemy, I am getting the following error:
NoSuchTableError: <table name>
I have used the following commands:
db = create_engine('oracle://username:password@hostname:1521/instance name', echo='debug')
md = MetaData(bind=db)
t = Table('table name', md, autoload=True, schema='schema name')
When I use the following command:
t= Table('table name', md, autoload=True, oracle_resolve_synonyms=True)
I get the following error:
AssertionError: There are multiple tables visible to the schema, you must specify owner
Can you please explain where exactly I am going wrong?
SQLAlchemy (The Python SQL Toolkit and Object Relational Mapper) allow Oracle connection through the cx_oracle driver. This how-to describes how to install SQLAlchemy for Oracle Database and how to integrate it in buildout and use it in a browser view.
Supported Databases. SQLAlchemy includes dialects for SQLite, Postgresql, MySQL, Oracle, MS-SQL, Firebird, Sybase and others, most of which support multiple DBAPIs.
One of the key aspects of any data science workflow is the sourcing, cleaning, and storing of raw data in a form that can be used upstream. This process is commonly referred to as “Extract-Transform-Load,” or ETL for short.
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)
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.
Assuming you have the Oracle client on your machine with a valid tnsnames.ora file, this works for me:
from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('oracle://myusername:mypassword@SID')
con = engine.connect()
outpt = con.execute("SELECT * FROM YOUR_TABLE")
df = pd.DataFrame(outpt.fetchall())
df.columns = outpt.keys()
print(df.head())
con.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With