【问题标题】:Using JPype - How can I access JDBC Meta Data Functions使用 JPype - 如何访问 JDBC 元数据函数
【发布时间】:2014-01-28 10:58:38
【问题描述】:

我正在使用JayDeBeAPI,它使用 JPype 加载 FileMaker 的 JDBC 驱动程序并提取数据。

但我也希望能够获取数据库中所有表的列表

JDBC documentation(第55页)中列出了以下功能:

JDBC 客户端驱动支持以下元数据功能:

获取列

getColumnPrivileges

获取元数据

获取类型信息

获取表格

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)

标签: python jdbc jpype jaydebeapi


【解决方案1】:

来自 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"} );

获取模式中的所有表(并且仅表)。

问候。

【讨论】:

    【解决方案2】:

    好的,感谢 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']
    

    【讨论】:

    • 不,不要删除它。这真的很有用。小更正:您将 conn 设置为您的连接,然后您一直引用 souce_conn。我假设您想设置 source_conn 而不是 conn(第 9 行)。我还有另一个问题(可能我使用的是不同版本的 jpype 或 jaydebeapi);我需要按如下方式设置游标 _meta 属性:table_reader_cursor._meta = results.getMetaData()。如果我不这样做,我会在尝试调用 _meta 上的 getColumncount 时收到 AttributeError。除此之外,你真的帮了我大忙!
    • 您也可以通过 self.cursor._meta 属性中的特定请求访问元数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2016-06-18
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多