Pandas Dataframe 在括号内用逗号显示结果

问题描述

我正在使用 create or replace PROCEDURE InsertUser(.. params ...) as temp_id number; BEGIN insert into user(User_Id,userName,...) values(...) returning User_Id into temp_id; insert into Password(User_Id,....) values(temp_id,...); insert into Userinformation(User_Id,...); commit; exception when others then rollback; END InsertUser; 将数据读入数据帧。这 数据库是 Oracle,Oracle 连接模块是 JayDeBeApi

问题: 连接成功,我也能够检索数据。 但是当我打印数据框时,它看起来与下面不同

pd.read_sql()

enter image description here

解决方法

我假设您将 jpypejaydebeapi 一起使用,因为我之前已经在此类设置中看到过这种确切的行为(另请参阅 this SO answer and the comments)。

您可以通过更改连接来处理症状(请参阅其他答案)或首先防止问题发生,例如以下两种方式之一:

  1. 使用 jaydebeapi 而不使用 jpype

    当这个问题发生在我身上时,我注意到使用 jpype 既是原因又是不必要的。我在本地将驱动程序作为 .jar 文件,并且我的连接在没有 jpype 的情况下运行良好:

    import jaydebeapi as jdba
    import pandas as pd
    import os
    
    db_host = 'db.host.com'
    db_port = 1521
    db_sid = 'YOURSID'
    
    jar=os.getcwd()+'/ojdbc6.jar'
    
    conn = jdba.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@' + db_host + ':' + str(db_port) + ':' + db_sid,{'user': 'USERNAME','password': 'PASSWORD'},jar
                    )
    
    df_jay = pd.read_sql('SELECT * FROM YOURSID.table1',conn)
    
    conn.close()
    

    这正确创建了数据框。

  2. 改用cx_Oracle

    如果我使用 cx_Oracle 连接到 Oracle 数据库也不会出现此问题:

    import cx_Oracle
    import pandas as pd
    import os
    
    db_host = 'db.host.com'
    db_port = 1521
    db_sid = 'YOURSID'
    
    dsn_tns = cx_Oracle.makedsn(db_host,db_port,db_sid)
    cx_conn = cx_Oracle.connect('USERNAME','PASSWORD',dsn_tns)
    
    df_cxo = pd.read_sql('SELECT * FROM YOURSID.table1',con=cx_conn)
    
    cx_conn.close()
    

    注意:要使 cx_Oracle 工作,您必须安装并正确设置 Oracle Instant Client(参见例如 cx_Oracle documentation for Ubuntu)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...