explaingit

kennethreitz/records

7,223PythonAudience · dataComplexity · 2/5Setup · easy

TLDR

Records is a Python library for running raw SQL queries against any major database and exporting results to CSV, JSON, Excel, or Pandas in just a few lines, no ORM needed.

Mindmap

mindmap
  root((Records))
    Querying
      Raw SQL strings
      SQL from file
      Safe parameters
    Results
      Column name access
      Index access
      Cached rows
    Export formats
      CSV and JSON
      Excel
      Pandas DataFrame
    Databases
      PostgreSQL
      MySQL
      SQLite
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Export a SQL query result directly to a CSV or Excel file with a single line of Python.

USE CASE 2

Run parameterized SQL queries against PostgreSQL or SQLite without setting up an ORM.

USE CASE 3

Build a reporting script that queries a database and loads results into a Pandas DataFrame for analysis.

Tech stack

PythonSQLAlchemyTablibPostgreSQLMySQLSQLite

Getting it running

Difficulty · easy Time to first run · 5min

Requires a separate database driver (e.g., psycopg2 for PostgreSQL) installed alongside Records.

In plain English

Records is a Python library for running raw SQL queries against relational databases with as little setup as possible. It is designed for people who already know SQL and want to write queries directly rather than learning a separate object-relational mapping framework. The library handles the connection and wraps the results in convenient row objects, then stays out of the way. Connecting to a database takes one line using a standard URL string. Queries are passed as plain SQL strings or loaded from a file. Results can be iterated, accessed by index, or accessed by column name using simple attribute syntax such as row.email. Safe query parameterization is supported to avoid injection risks. Rows are cached after the first iteration so the result set can be accessed multiple times without hitting the database again. Transactions and bulk inserts are also available. One of the more useful features is the built-in export capability. Query results can be exported in a single line to CSV, JSON, YAML, HTML tables, Excel files, or Pandas DataFrames. This is powered by the Tablib library under the hood. A command-line tool is also included for running queries and exporting results without writing any Python code. The library works with PostgreSQL, MySQL, SQLite, Oracle, Microsoft SQL Server, and Amazon Redshift. Database drivers are not included and need to be installed separately for the specific database being used. The underlying database connection is managed by SQLAlchemy, which handles the compatibility across all those databases. Records is a straightforward utility for situations where a full ORM would add unnecessary complexity. It is well suited to data exports, reporting scripts, and one-off analysis tasks where SQL is already the natural language and the goal is just to get the results out cleanly.

Copy-paste prompts

Prompt 1
Using the Records library, write Python code that connects to a PostgreSQL database, runs a SELECT query with a parameter, and exports the results as a CSV file.
Prompt 2
Show me how to use Records to query SQLite and access result rows by column name instead of index.
Prompt 3
I want to run a SQL query and export the results to an Excel file using Records. Show me the minimal code.
Prompt 4
How does Records handle query parameterization to prevent SQL injection? Show me an example with a user-supplied value.
Open on GitHub → Explain another repo

← kennethreitz on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.