Utilizzo di JPYPY - Come posso accedere alle funzioni JDBC Meta Data
-
21-12-2019 - |
Domanda
Sto usando jaydebeapi che utilizza jpype per caricare il driver JDBC di FileMaker e tirare i dati.
Ma voglio anche essere in grado di Ottieni un elenco di tutte le tabelle nel database.
in Documentazione JDBC (pagina 55)Elenca le seguenti funzioni:
.Il driver client JDBC supporta le seguenti funzioni Meta Data:
GetColumns
GetColumnPrivileges
getmetadata
getypeinfo
GetTables
GetTableTypes
Qualche idea come potrei chiamarli da JPype o Jaydebeapi?
Se aiuta, ecco il mio codice corrente:
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()
.
Aggiornamento:
Ecco alcuni progressi e sembra che dovrebbe funzionare, ma sto ottenendo l'errore qui sotto.Qualche idea?
> conn.jconn.metadata.getTables()
*** RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121
. Soluzione
OK, grazie a Eltabo e Juan Mellado l'ho capito!
Ho appena dovuto passare nei parametri corretti per abbinare la firma del metodo.
Ecco il codice di lavoro:
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']
. Altri suggerimenti
Dal risultati Javadoc:
public ResultSet getTables(String catalog,
String schemaPattern,
String tableNamePattern,
String[] types)
throws SQLException
.
È necessario passare i quattro parametri al metodo.Non sono uno sviluppatore di Python, ma in Java I Uso:
ResultSet rs = metadata.getTables(null, "public", "%" ,new String[] {"TABLE"} );
.
Per ottenere tutte le tabelle (e solo le tabelle) in uno schema.
Saluti.