agate-sql

0.7.3 · active · verified Wed Apr 15

agate-sql extends the `agate` data analysis library by adding robust SQL read/write capabilities. It enables users to import data from SQL databases into `agate.Table` objects and export `agate.Table` objects back to SQL databases. The current version is 0.7.3. It maintains a consistent release cadence, often aligned with `agate` updates, and introduces new features, bug fixes, and Python version support.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an `agate.Table`, write it to a SQL database, and then read data back using `from_sql` and `from_sql_query`. It uses an in-memory SQLite database by default but can be configured for other databases via an environment variable. Remember to install the necessary SQLAlchemy database drivers for your chosen database (e.g., `psycopg2-binary` for PostgreSQL).

import agate
import agatesql
import os

# This example assumes a PostgreSQL database named 'test_db' with a table 'my_data'.
# For a real scenario, replace with your database connection string and table name.
# Ensure the appropriate SQLAlchemy driver (e.g., psycopg2) is installed.

# Create a dummy agate table for demonstration
data = [
    ['apple', 10],
    ['banana', 20],
    ['cherry', 30]
]
column_names = ['fruit', 'quantity']
column_types = [agate.Text(), agate.Number()]

table = agate.Table(data, column_names, column_types)

# --- Writing to SQL (example using a dummy connection string) ---
# In a real application, use a proper connection string like 'postgresql:///mydb'
# and ensure your database is running and credentials are correct.
# For testing, you might use an in-memory SQLite database:
# SQLALCHEMY_DATABASE_URL = 'sqlite:///:memory:'
SQLALCHEMY_DATABASE_URL = os.environ.get('SQLALCHEMY_DATABASE_URL', 'sqlite:///:memory:')
DB_TABLE_NAME = os.environ.get('DB_TABLE_NAME', 'test_fruits')

try:
    print(f"Attempting to write table '{DB_TABLE_NAME}' to {SQLALCHEMY_DATABASE_URL}...")
    table.to_sql(SQLALCHEMY_DATABASE_URL, DB_TABLE_NAME, overwrite=True)
    print("Table written successfully.")

    # --- Reading from SQL ---
    print(f"Attempting to read table '{DB_TABLE_NAME}' from {SQLALCHEMY_DATABASE_URL}...")
    new_table = agate.Table.from_sql(SQLALCHEMY_DATABASE_URL, DB_TABLE_NAME)
    print("Table read successfully. Contents:")
    new_table.print_table()

    # --- Reading from a SQL query (if the dialect supports it) ---
    # Note: Column types will be inferred from the returned data, not SQL schema.
    print(f"Attempting to read from SQL query on '{DB_TABLE_NAME}'...")
    query_table = agate.Table.from_sql_query(SQLALCHEMY_DATABASE_URL, f"SELECT fruit, quantity FROM {DB_TABLE_NAME} WHERE quantity > 15")
    print("Query results:")
    query_table.print_table()

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have the correct SQLAlchemy drivers installed for your database (e.g., `pip install psycopg2-binary` for PostgreSQL) and your database is accessible.")

view raw JSON →