I'm trying to access my Oracle 11g(r2) Express Edition database via python. I specifically want to create a pandas DataFrame from one of the tables, HISTORY_FULLNESS. However, when I try to use the pandas.read_sql_table() function, it gives me a Table Not Found error. In the code below I duplicate the query that the engine is executing, and it clearly has HISTORY_FULLNESS in the results. Any clue what I'm doing wrong?
I have the following code (*** added to obfuscate ip and password):
### Imports ###
import pandas as pd
import numpy as np
import cx_Oracle
import pandas.io.sql as psql
import sqlalchemy
# Open connection to Oracle Database
OpsDBengine = sqlalchemy.create_engine('oracle://OPSDBADMIN:***@***.***.***.***:1521/', echo=True)
connection = OpsDBengine.connect()
result = connection.execute("SELECT table_name FROM all_tables WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX') AND OWNER = 'OPSDBADMIN' AND IOT_NAME IS NULL")
for row in result:
print "tablename: ", row['table_name']
testDF = pd.read_sql_table(table_name = 'HISTORY_FULLNESS', con=OpsDBengine, schema='OPSDBADMIN', index_col = 'FULLNESSRECORDID')
The output is as follows:
2015-02-19 15:23:58,714 INFO sqlalchemy.engine.base.Engine SELECT USER FROM DUAL
INFO:sqlalchemy.engine.base.Engine:SELECT USER FROM DUAL
2015-02-19 15:23:58,716 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}
2015-02-19 15:23:58,740 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60 CHAR)) AS anon_1 FROM DUAL
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test plain returns' AS VARCHAR(60 CHAR)) AS anon_1 FROM DUAL
2015-02-19 15:23:58,740 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}
2015-02-19 15:23:58,746 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS NVARCHAR2(60)) AS anon_1 FROM DUAL
INFO:sqlalchemy.engine.base.Engine:SELECT CAST('test unicode returns' AS NVARCHAR2(60)) AS anon_1 FROM DUAL
2015-02-19 15:23:58,749 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}
2015-02-19 15:23:58,766 INFO sqlalchemy.engine.base.Engine SELECT table_name FROM all_tables WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX') AND OWNER = 'OPSDBADMIN' AND IOT_NAME IS NULL
INFO:sqlalchemy.engine.base.Engine:SELECT table_name FROM all_tables WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX') AND OWNER = 'OPSDBADMIN' AND IOT_NAME IS NULL
2015-02-19 15:23:58,766 INFO sqlalchemy.engine.base.Engine {}
INFO:sqlalchemy.engine.base.Engine:{}
tablename: SAFETY_CASEFILE
tablename: HTMLDB_PLAN_TABLE
tablename: HISTORY_FULLNESS
tablename: HR_EMPLOYEE_PHOTOS
tablename: HR_MANAGER_DEPARTMENT_GROUPS
tablename: DB_DEPARTMENT_GROUPS
tablename: HR_MANAGER_DEPT_ASSIGNMENTS
tablename: HR_MANAGER_SHIFT_ASSIGNMENTS
tablename: HR_EMPLOYEE_ROLE
2015-02-19 15:23:58,825 INFO sqlalchemy.engine.base.Engine SELECT table_name FROM all_tables WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX') AND OWNER = :owner AND IOT_NAME IS NULL
INFO:sqlalchemy.engine.base.Engine:SELECT table_name FROM all_tables WHERE nvl(tablespace_name, 'no tablespace') NOT IN ('SYSTEM', 'SYSAUX') AND OWNER = :owner AND IOT_NAME IS NULL
2015-02-19 15:23:58,825 INFO sqlalchemy.engine.base.Engine {'owner': u'OPSDBADMIN'}
INFO:sqlalchemy.engine.base.Engine:{'owner': u'OPSDBADMIN'}
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-8f6c6d72ed22> in <module>()
15
16
---> 17 testDF = pd.read_sql_table(table_name = 'HISTORY_FULLNESS', con=OpsDBengine, schema='OPSDBADMIN', index_col = 'FULLNESSRECORDID')
C:\Anaconda\lib\site-packages\pandas\io\sql.pyc in read_sql_table(table_name, con, schema, index_col, coerce_float, parse_dates, columns, chunksize)
334 meta.reflect(only=[table_name])
335 except sqlalchemy.exc.InvalidRequestError:
--> 336 raise ValueError("Table %s not found" % table_name)
337
338 pandas_sql = SQLDatabase(con, meta=meta)
ValueError: Table HISTORY_FULLNESS not found
Kinda old question, but could help others:
You could use 2 ways to do this:
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