https://github.com/bilbottom/database-exporter
Export database query result sets.
https://github.com/bilbottom/database-exporter
Last synced: over 1 year ago
JSON representation
Export database query result sets.
- Host: GitHub
- URL: https://github.com/bilbottom/database-exporter
- Owner: Bilbottom
- Created: 2024-11-22T09:33:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-08T20:55:30.000Z (over 1 year ago)
- Last Synced: 2025-01-30T03:44:23.962Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[](https://www.python.org/downloads/release/python-3110/)
[](https://python-poetry.org/)
[](https://github.com/Bilbottom/database-exporter/actions/workflows/tests.yaml)
[](https://github.com/dbrgn/coverage-badge)
[](https://shields.io/badges/git-hub-last-commit)
[](https://github.com/prettier/prettier)
[](https://github.com/astral-sh/ruff)
[](https://results.pre-commit.ci/latest/github/Bilbottom/database-exporter/main)
---
# Database Exporter 📦📤
Export database query result sets.
This tool is database-agnostic -- just provide a class that connects to your database with an execute method, and the query whose result set you want to export.
## Installation ⬇️
While in preview, this package is only available from GitHub:
```
pip install git+https://github.com/Bilbottom/database-exporter@v0.0.1
```
This will be made available on PyPI once it's ready for general use.
## Usage 📖
The package exposes functions which require a database connection/cursor class that implements an `execute` method.
### SQLite Example
> Official documentation: https://docs.python.org/3/library/sqlite3.html
```python
import pathlib
import sqlite3
import database_exporter
def main() -> None:
db_conn = sqlite3.connect(":memory:") # Or a path to a database file
query_path = pathlib.Path("path/to/query.sql")
database_exporter.query_to_csv(
conn=db_conn,
query=query_path.read_text("utf-8"),
filepath=query_path.with_suffix(".csv"),
)
if __name__ == "__main__":
main()
```
### Snowflake Example
> Official documentation: https://docs.snowflake.com/en/developer-guide/python-connector/python-connector-example
```python
import pathlib
import database_exporter
import snowflake.connector # snowflake-connector-python
# This dictionary is just for illustration purposes, and
# you should use whatever connection method you prefer
CREDENTIALS = {
"user": "XXX",
"password": "XXX",
"account": "XXX",
"warehouse": "XXX",
"role": "XXX",
"database": "XXX",
}
def main() -> None:
db_conn = snowflake.connector.SnowflakeConnection(**CREDENTIALS)
query_path = pathlib.Path("path/to/query.sql")
with db_conn.cursor() as cursor:
database_exporter.query_to_csv(
conn=cursor,
query=query_path.read_text("utf-8"),
filepath=query_path.with_suffix(".csv"),
)
db_conn.close()
if __name__ == "__main__":
main()
```