Skip to main content

Connect to databases from Python

Project description

Cmotions DatabaseUtils

cmo_databaseutils is a Python library created by The Analytics Lab, which is powered by Cmotions. This library combines our most used functionality when it comes to connecting to databases from Python. Nothing fancy, but very useful for our consultants. Mostly since this package is used in other packages, which makes our workflow simpler and more efficient.

Since we love to share what we do, why not also do that with our packages, that is why we've decided to make (almost) all of our packages open source, this way we hope to give back to the community that brings us so much. Enjoy!

Installation

Install cmo_databaseutils using pip

pip install cmo-databaseutils

If you want to be able to use the tutorial notebook or the code below, you also need to install pydataset and dotenv using pip

pip install python-dotenv
pip install pydataset

Usage

import os
from pydataset import data
from dotenv import load_dotenv
from cmo_databaseutils import db_utils

load_dotenv()

df = data("iris")

# create a database connection to SQL Server using the credentials provided in a .env file
db_conn = db_utils(server=os.getenv("DB1_SERVER"), db=os.getenv("DB1_DATABASE"), server_name=os.getenv("DB1_SERVER_NAME"), usr=os.getenv("DB1_USERNAME"), pwd=os.getenv("DB1_PASSWORD"), driver=os.getenv("DB1_DRIVER"), port=os.getenv("DB1_PORT"))

# create a database connection to SQL Server using Windows credentials
db_conn = db_utils(server="localhost", db="localtemp")

# get an overview of all active schemas in the database
db_conn.list_all_active_schemas()

# get an overview of all tables in the database within the specified schemas
db_conn.list_all_tables(schema=['dbo'])

# load the data from an entire table
cars = db_conn.retrieve_table_from_sql(table="cars", schema="dbo")

# write a table to the SQL database, when possible the package will use BCP to optimize the efficiency of the process
db_conn.write_dataframe_to_sql(data=df, table="mynewtable", schema="dbo", replace_if_exists=False, index=False, dtype=None)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

GNU General Public License v3.0

Contributors

Jeanine Schoonemann
Contact us

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

cmo_databaseutils-0.0.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

cmo_databaseutils-0.0.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file cmo_databaseutils-0.0.2.tar.gz.

File metadata

  • Download URL: cmo_databaseutils-0.0.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/5.1.0 pkginfo/1.9.6 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.9.1

File hashes

Hashes for cmo_databaseutils-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1615f1b0711a6b4ce65212753de9900f9ced0054f3b5977c95ca40b10a017fc9
MD5 8e944214364d0549f354ef41ae764eb7
BLAKE2b-256 bde24485ecd4f317ca329f821a74f0a1212a9e242f5477e017afcc179ee0e016

See more details on using hashes here.

File details

Details for the file cmo_databaseutils-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: cmo_databaseutils-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/5.1.0 pkginfo/1.9.6 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.65.0 CPython/3.9.1

File hashes

Hashes for cmo_databaseutils-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 90cd4621aa1bba592a5c0b655595e35971f85e97f71b0770971a7e146533db54
MD5 688ad33ec29d8a9497b544af1de181d1
BLAKE2b-256 67e1bc5d58297f0a42f08044a764064418c55057214c31d765c7ad51e7bd8e12

See more details on using hashes here.

Supported by

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