Application Development

Application development with MariaDB Connector/Python

Field Information

MariaDB Connector/Python provides the Fieldinfo class for retrieving data type and flag information on table columns in the database.

The following example shows how to get the field information for the example table created in Setup for Examples:

Retrieving Field Information for Query Results

To retrieve field information for query results:

  1. Import MariaDB Connector/Python:

    # Module Import
    import mariadb
  2. Define a select_contacts() function that retrieves all contacts from the table:

    # Print List of Contacts
    def select_contacts(cur):
       """Retrieves the list of contacts from the database"""
    
       # Retrieve Contacts
       cur.execute("SELECT first_name, last_name, email FROM test.contacts")
  3. Define a get_field_info() function that prints the field information associated with the cursor:

    # Get field info from cursor
    def get_field_info(cur):
       """Retrieves the field info associated with a cursor"""
    
       field_info = mariadb.fieldinfo()
    
       field_info_text = []
    
       # Retrieve Column Information
       for column in cur.description:
          column_name = column[0]
          column_type = field_info.type(column)
          column_flags = field_info.flag(column)
    
          field_info_text.append(f"{column_name}: {column_type} {column_flags}")
    
       return field_info_text
  4. Call these functions, then print the field information:

The results should look like this:

Retrieve Field Information for All Tables

To retrieve field information for all tables:

  1. Import MariaDB Connector/Python:

  2. Define a show_tables() function that executes the SHOW TABLES statement:

  3. Define a get_field_info() function that prints the field information associated with the cursor:

  4. Define a get_table_field_info() function that prints the field information associated with a table:

  5. Call these functions, and then print the field information for each table:

This page is: Copyright © 2025 MariaDB. All rights reserved.

Last updated

Was this helpful?