HDX Python database utilities
Project description
The HDX Python Database Library provides utilities for connecting to databases in a standardised way including through an ssh tunnel if needed. It is built on top of SQLAlchemy and simplifies its setup. Additional postgres functionality is available if this library is installed with:
pip install hdx-python-database[postgres]
This library is part of the Humanitarian Data Exchange (HDX) project. If you have humanitarian related data, please upload your datasets to HDX.
Usage
The library has detailed API documentation which can be found here: http://ocha-dap.github.io/hdx-python-database/. The code for the library is here: https://github.com/ocha-dap/hdx-python-database.
Examples
Your SQLAlchemy database tables must inherit from Base in hdx.utilities.database eg. :
from hdx.database import Base
class MyTable(Base):
my_col = Column(Integer, ForeignKey(MyTable2.col2), primary_key=True)
Examples:
# Get SQLAlchemy session object given database parameters and
# if needed SSH parameters. If database is PostgreSQL, will poll
# till it is up.
from hdx.database import Database
with Database(database='db', host='1.2.3.4', username='user', password='pass',
driver='driver', ssh_host='5.6.7.8', ssh_port=2222,
ssh_username='sshuser', ssh_private_key='path_to_key') as session:
session.query(...)
# Extract dictionary of parameters from SQLAlchemy url
result = Database.get_params_from_sqlalchemy_url(TestDatabase.sqlalchemy_url)
# Build SQLAlchemy url from dictionary of parameters
result = Database.get_sqlalchemy_url(**TestDatabase.params)
# Wait util PostgreSQL is up
# Library should be installed with hdx-python-database[postgres]
wait_for_postgres('mydatabase', 'myserver', 5432, 'myuser', 'mypass')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hdx-python-database-1.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88421f782bb66b8c67c911ee988569e7a5cc15207953567833c7b1dbf923b889 |
|
MD5 | 5febb37d016b91bfaacd315f6f96a09c |
|
BLAKE2b-256 | 66590172c176e4e2b906ed42a97605de6cabc06242e37843d51aa1855221834e |
Hashes for hdx_python_database-1.0.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad668e75e50cf7d900ca38a8d600ce232eeb702cc81e59592a28dc716a627545 |
|
MD5 | 8172e18cc3834d4ea5e9e57cad9f8562 |
|
BLAKE2b-256 | 8ae53df01e4f6d6b81ae161a9652738394a6a462453fd0e170f0dee400b0bf79 |