pandas.read_sql_table 生成表未找到错误

Posted

技术标签:

【中文标题】pandas.read_sql_table 生成表未找到错误【英文标题】:pandas.read_sql_table producing table not found error 【发布时间】:2015-04-21 13:05:37 【问题描述】:

我正在尝试通过 python 访问我的 Oracle 11g(r2) Express Edition 数据库。我特别想从其中一个表 HISTORY_FULLNESS 创建一个 pandas DataFrame。但是,当我尝试使用 pandas.read_sql_table() 函数时,它给了我一个 Table Not Found 错误。在下面的代码中,我复制了引擎正在执行的查询,结果中显然有 HISTORY_FULLNESS。任何线索我做错了什么?

我有以下代码(***添加到混淆 ip 和密码):

### 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 = 'FULLNEs-s-rECORDID')

输出如下:

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 = 'FULLNEs-s-rECORDID')

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

【问题讨论】:

以下行确实有效:testDF = pd.read_sql_query("SELECT * FROM HISTORY_FULLNESS", con=OpsDBengine) 【参考方案1】:

有点老问题,但可以帮助其他人:

您可以使用两种方法来做到这一点:

    testDF = pd.read_sql_query("SELECT * FROM HISTORY_FULLNESS", con=OpsDBengine) output = con.execute("SELECT * FROM HISTORY_FULLNESS"); testDF = pd.DataFrame(outpt.fetchall());

【讨论】:

【参考方案2】:

我意识到我正在发布一个非常古老的问题,但我在 oracle 上遇到了同样的问题。您是否尝试过将表名设为小写?这对我有用。

https://github.com/pandas-dev/pandas/issues/11258

### 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'.lower(), con=OpsDBengine, schema='OPSDBADMIN', index_col = 'FULLNEs-s-rECORDID')

【讨论】:

以上是关于pandas.read_sql_table 生成表未找到错误的主要内容,如果未能解决你的问题,请参考以下文章

数据库怎么自动生成关系表?

生成多个文件表结构并创建表

MySQL 生成日期表

从0到1打造代码生成器数据库表生成Java实体类

动态表名称生成导致“找不到表”

访问生成表查询