使用JPype - 如何访问JDBC元数据函数

3
我正在使用JayDeBeAPI,该工具使用JPype来加载FileMaker的JDBC驱动程序并提取数据。
但我也想能够获取数据库中所有表的列表
JDBC文档(第55页)中,列出了以下函数:

JDBC客户端驱动程序支持以下元数据函数:

getColumns

getColumnPrivileges

getMetaData

getTypeInfo

getTables

getTableTypes

有什么想法可以从JPype或JayDeBeAPI中调用它们吗?
如果有帮助的话,这是我的当前代码:
import jaydebeapi
import jpype

jar = r'/opt/drivers/fmjdbc.jar'
args='-Djava.class.path=%s' % jar
jvm_path = jpype.getDefaultJVMPath()
jpype.startJVM(jvm_path, args)

conn = jaydebeapi.connect('com.filemaker.jdbc.Driver',
        SETTINGS['SOURCE_URL'], SETTINGS['SOURCE_UID'], SETTINGS['SOURCE_PW'])
curs = conn.cursor()

#Sample Query:
curs.execute("select * from table")
result_rows = curs.fetchall()

更新:

这里有一些进展,看起来应该可以工作,但是我收到了下面的错误。有任何想法吗?

> conn.jconn.metadata.getTables()
*** RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121

我现在无法进行测试,但我认为调用应该与Java的getTables方法签名匹配,那么怎么样:conn.jconn.getMetadata().getTables(None, None, "%", None) - Juan Mellado
2个回答

5

感谢 eltabo 和 Juan Mellado 的帮助,我终于解决了问题!

我只需要传入正确的参数以匹配方法签名。

以下是可正常工作的代码:

import jaydebeapi
import jpype

jar = r'/opt/drivers/fmjdbc.jar'
args='-Djava.class.path=%s' % jar
jvm_path = jpype.getDefaultJVMPath()
jpype.startJVM(jvm_path, args)

conn = jaydebeapi.connect('com.filemaker.jdbc.Driver',
        SETTINGS['SOURCE_URL'], SETTINGS['SOURCE_UID'], SETTINGS['SOURCE_PW'])
results = source_conn.jconn.getMetaData().getTables(None, None, "%", None)

#I'm not sure if this is how to read the result set, but jaydebeapi's cursor object
# has a lot of logic for getting information out of a result set, so let's harness
# that.
table_reader_cursor = source_conn.cursor()
table_reader_cursor._rs = results
read_results = table_reader_cursor.fetchall()
#get just the table names
[row[2] for row in read_results if row[3]=='TABLE']

1
不要删除它。这是非常有用的。小修正:您将conn设置为连接,稍后您始终引用souce_conn。我假设您想设置source_conn而不是conn(第9行)。我的另一个问题是(也许我正在使用不同版本的jpype或jaydebeapi); 我需要将游标的_meta属性设置如下:table_reader_cursor._meta = results.getMetaData()。如果我不这样做,当它尝试在_meta上调用getColumncount时,我会得到AttributeError。除此之外,您真的帮了我一个大忙! - HSquirrel
您可以从self.cursor._meta属性中访问特定请求的元数据。 - sakurashinken
@Greg,我该如何使用jaydebeapi在Python中获取数据库的视图、存储过程、触发器和函数列表? - Sony Khan

3

来自 ResultSet Javadoc:

public ResultSet getTables(String catalog,
                       String schemaPattern,
                       String tableNamePattern,
                       String[] types)
                throws SQLException

您需要将四个参数传递给该方法。虽然我不是Python开发者,但在Java中,我使用以下代码:
ResultSet rs = metadata.getTables(null, "public", "%" ,new String[] {"TABLE"} );

获取特定数据库模式中的所有表(仅限表)。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接