gpt4 book ai didi

python - pandas.read_sql_table 生成表未找到错误

转载 作者:行者123 更新时间:2023-12-03 17:30:03 25 4
gpt4 key购买 nike

我正在尝试通过 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 = 'FULLNESSRECORDID')

输出如下:
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

最佳答案

有点老问题,但可以帮助其他人:
您可以使用 2 种方法来执行此操作:

  • testDF = pd.read_sql_query("SELECT * FROM HISTORY_FULLNESS", con=OpsDBengine)
  • output = con.execute("SELECT * FROM HISTORY_FULLNESS"); testDF = pd.DataFrame(outpt.fetchall());
  • 关于python - pandas.read_sql_table 生成表未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619045/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com