- Connecting to the Database
- Cursor
- zxJDBC and MetaData
- Prepared Statements
- Errors and Warnings
- dbexts
- About This Article
zxJDBC and MetaData
The Python DB API does not contain metadata specifications, but zxJDBC does provide some connection metadata with a number of connection and cursor attributes. These attributes match bean properties found in the JDBC java.sql.DatabaseMetaData object. Table 2 shows the zxJDBC cursor fields and the underlying java.sql.DatabaseMetaData bean methods.
Table 2zxJDBC MetaData
zxJDBC Attribute |
DatabaseMetaData Accessor |
connection.dbname |
|
connection.dbversion |
|
cursor.tables(catalog, schemapattern, tablepattern, types) |
|
cursor.columns(catalog, schemapattern, tablenamepattern, columnnamepattern) |
|
cursor.foreignkeys(primarycatalog, primaryschema, pimarytable, foreigncatalog, foreignschema, foreigntable) |
|
cursor.primarykeys(catalog, schema, table) |
|
cursor.procedures(catalog, schemapattern, procedurepattern) |
|
cursor.procedurecolumns(catalog, schemapattern, procedurepattern, columnpattern) |
|
cursor.statistics(catalog, schema, table, unique, approximation) |
Here is an example of extracting some metadata from the MySQL random database:
>>> from com.ziclix.python.sql import zxJDBC >>> url = "jdbc:mysql://localhost/test" >>> driver = "org.gjt.mm.mysql.Driver" >>> dbconn = zxJDBC.connect(url, "jyuser", "beans", driver) >>> dbconn.dbname 'MySQL' >>> dbconn.dbversion '3.23.32'
The remaining metadata is accessible through a cursor object. When the cursor retrieves information, it stores it internally waiting for the user to fetch it. To view metadata provided by the cursor, call each metadata method, then use the cursor to retrieve the data as shown here:
>>> cursor.primarykeys(None, "%", "random") >>> cursor.fetchall() [('', '', 'random', 'pkey', 1, 'pkey')] >>> cursor.tables(None, None, "%", None) >>> cursor.fetchall() [('', '', 'random', 'TABLE', '')] >>> cursor.primarykeys('test', '%', 'random') >>> cursor.fetchall() [('test', '', 'random', 'pkey', 1, 'pkey')] >>> cursor.statistics('test', '', 'random', 0, 1) >>> cursor.fetchall() [('test', '', 'random', 'false', '', 'PRIMARY', '3', 1, 'pkey', 'A', '23', None, '')]