我正在使用以下代码连接到Oracle数据库:
jar = ojdbc8.jar path
jvm_path = jvm.dll path
args = '-Djava.class.path=%s' % jar
jpype.startJVM(jvm_path, args)
con = jaydebeapi.connect("oracle.jdbc.driver.OracleDriver", url,[user, password], jar)
连接工作正常,但返回的数据是this odd format
pd.read_sql("SELECT * FROM table1", con)
屈服
+---+-----------------+-----------------+-----------------+
| | (C,O,L,U,M,N,1) | (C,O,L,U,M,N,2) | (C,O,L,U,M,N,3) |
+---+-----------------+-----------------+-----------------+
| 1 | (t,e,s,t) | (t,e,s,t,2) | 1 |
+---+-----------------+-----------------+-----------------+
| 2 | (f,o,o) | (b,a,r) | 100 |
+---+-----------------+-----------------+-----------------+
正确导入了编号和日期,但没有导入varchar
列。我试过不同的桌子,他们都有这个问题
我在任何地方都没见过这样的事。希望你能帮助我
当将
jaydebeapi
与jpype
一起使用时,这似乎是一个问题。当连接到Oracle db时,我可以用与您相同的方式重现这种情况(在我的例子中是Oracle 11gR2,但由于您使用的是ojdbc8.jar
,我想其他版本也会出现这种情况)有不同的方法可以解决此问题:
更改您的连接
由于错误似乎只发生在特定的包组合中,因此最明智的做法是尝试避免这些错误,从而避免整个错误
备选方案1:使用
jaydebeapi
而不使用jpype
:如前所述,我仅在将
jaydebeapi
与jpype
一起使用时观察到这一点。然而,在我的例子中,根本不需要jpype
。我在本地有.jar
文件,没有它我的连接可以正常工作:在我的例子中,这可以正常工作并创建数据帧
备选方案2:使用
cx_Oracle
代替:如果我使用
cx_Oracle
连接到Oracle数据库,也不会出现此问题:注意:要使
cx_Oracle
工作,您必须安装Oracle Instant Client并正确设置(参见例如cx_Oracle documentation for Ubuntu)修复事实后的数据帧:
如果出于某种原因,无法使用上述连接替代方案,还可以转换数据帧
备选方案3:联接元组条目:
您可以使用
''.join()
到convert tuples to strings。您需要对条目和列名执行此操作备选方案4:更改列的数据类型:
通过将受影响列的
dtype
从object
更改为string
,所有条目也将被转换。请注意,这可能会产生不必要的副作用,例如将None
值更改为字符串<N/A>
。此外,您还必须单独重命名列标题,如上所述所有这些最终都应该产生大致相同的
df
(除了dtypes
和None
值的可能替换之外):相关问题 更多 >
编程相关推荐