Skip to main content

A simple Python wrapper for DuckDB with SQL file module loading

Project description

QuackSQL

A simple Python wrapper for DuckDB that allows you to load and execute SQL queries from files with a clean, Pythonic interface.

Installation

pip install quacksql

Quick Start

import quacksql

# Connect to an in-memory database (default)
quacksql.connect()

# Or connect to a file
quacksql.connect('my_database.duckdb')

# Load SQL queries from a directory
quacksql.module('./queries')

# Execute a query (assumes you have a file queries/get_users.sql)
results = quacksql.get_users()

# Get results as a pandas DataFrame
df = quacksql.get_users().df()

# Pass parameters to queries
results = quacksql.get_user_by_id(user_id=123).df()

Features

  • Simple API: Load SQL files and execute them as Python methods
  • Pandas Integration: Easy conversion to DataFrames with .df()
  • Parameter Support: Pass parameters to queries using positional or keyword arguments
  • DuckDB Power: Full access to DuckDB's analytical capabilities

Usage

Loading SQL Modules

Place your SQL files in a directory:

queries/
├── get_users.sql
├── get_orders.sql
└── update_inventory.sql

Load them in Python:

import quacksql

quacksql.connect('mydb.duckdb')
quacksql.module('./queries')

# Now you can call any query as a method
users = quacksql.get_users().df()
orders = quacksql.get_orders().df()

Working with Parameters

SQL file (queries/get_user.sql):

SELECT * FROM users WHERE user_id = ?

Python code:

user = quacksql.get_user(123).fetchone()

Or with named parameters:

SELECT * FROM users WHERE name = $name AND age > $min_age
users = quacksql.get_users(name='Alice', min_age=25).df()

Query Results

All queries return a QueryResult object with multiple methods:

result = quacksql.my_query()

# Get as pandas DataFrame
df = result.df()

# Get all rows as list of tuples
rows = result.fetchall()

# Get first row
first = result.fetchone()

# Get n rows
some = result.fetchmany(10)

# Iterate over results
for row in result:
    print(row)

Requirements

  • Python >= 3.8
  • DuckDB
  • Pandas

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quacksql-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quacksql-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file quacksql-0.1.0.tar.gz.

File metadata

  • Download URL: quacksql-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for quacksql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ae34aac4b94ec7c3af6eb1e157b14a08366aea3b7d1c109e28a1122795645f2
MD5 a2fe877c31422464f011f09bd51eff73
BLAKE2b-256 6d5d93682e77c9f27d85f34042f94aead7cfc61429ec80b5a06c0ebb07531fdd

See more details on using hashes here.

File details

Details for the file quacksql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: quacksql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for quacksql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18e8bca3d3690cd23915403a6610c10f996f223fb6fc5c57619a987e713f17e4
MD5 938e8e97092034254624a0d327879065
BLAKE2b-256 4184baae5e1ed40560df1181ec3c2aef99ed83c1f7379db89de9117d45f82d88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page