Skip to main content

Python module for establishing a working relationship between relational databases and Pandas DataFrames

Project description

dbpd

The main theme of this module is to establish a working relationship between relational databases and Pandas DataFrames; because these objects are tabular in nature, it reveals itself to be an efficient way to inspect, manage and manipulate a given database.

The dbpd.BaseDBPD class is the parent class for six child classes that are specific to different database types:

dbpd.Access
dbpd.MySQL
dbpd.Oracle
dbpd.Postgres
dbpd.SQLite
dbpd.SQLiteInMemory

Abstractions have been created such that working with these various database types is consistent throughout the user's code base.

The query() method is responsible for returning SELECT sql statements as their respective DataFrames. However the query() method can also be used to make changes to the database (such as INSERT, UPDATE, DELETE, etc.)

Users are expected to write their own sql statements to query the database, and parameterized queries are accepted and encouraged as well.

This is not meant to be an Object-Relational-Mapper (ORM) and has no such functionality, although it may be possible for users to create their own ORM using the classes herein.

DOCUMENTATION https://zacharybeebe.github.io/dbpd/

Installation

pip install dbpd

Connecting to a Database #1 - Direct

from dbpd import Oracle

oracle = Oracle(
    username='example_username',
    password='example_password',
    host='127.0.0.1',
    sid='prod',
    port=5000,
    threaded=True,
    description='My Example Oracle database',
    show_description=True
)
dataframe = oracle.query('SELECT * FROM example_table')
oracle.close()

Connecting to a Database #2 - Inheritance

from dbpd import Postgres

class ExamplePostgres(Postgres):
    def __init__(self):
        super(ExamplePostgres, self).__init__(
            username='example_username',
            password='example_password',
            host='127.0.0.1',
            database_name='example',
            port=5000,
            postgres_schema='public',
            description='My Example Postgres database',
            show_description=True
        )
    
    def awesome_custom_method(self):
        print('I love pandas and databases')

pg = ExamplePostgres()
dataframe = pg.query('SELECT * FROM public.example_table')
pg.close()

Other Examples

from dbpd import Access, MySQL, SQLite, SQLiteInMemory

# Connect to existing Access Database
existing_access = Access(
    filepath='path/to/existing/access.accdb',
    fernet_encryption_key=b'<theFernetEncryptionKeyForYourDatabase>',
    description='My Existing Access database',
    show_description=True
)
existing_access.close()

#####################################################################
# Create new, blank Access Database
new_access = Access(
    filepath='path/to/non-existent/access.accdb',
    fernet_encryption_key=b'<theFernetEncryptionKeyForYourDatabase>',
    description='My New Access database',
    show_description=True
)
new_access.query(
    sql="""
    CREATE TABLE my_table (
        [a_number]  INTEGER,
        [a_date]    DATETIME,
        [a_double]  DOUBLE,
        [a_string]  VARCHAR
    );
    """
)
new_access.commit()
new_access.insert_values(
    table_name='my_table',
    a_number=1,
    a_date=new_access.dt_now(),
    a_double=22.22,
    a_string='HelloWorld'
)
new_access.commit()
new_access.close()

#####################################################################
# Connect to MySQL Database
mysql = MySQL(
    username='example_username',
    password='example_password',
    host='127.0.0.1',
    database_name='example',
    port=5000
)
# Export query to SQLite database
dataframe = mysql.export_query_to_sqlite(
    out_filepath='path/to/export/sqlite.db',
    out_table_name='exported_table',
    in_sql="""
        SELECT
            A.*,
            B.*
        FROM
            example_table A
        LEFT JOIN (
            SELECT
                *
            FROM
                other_table
        ) B ON A.id = B.id
        WHERE
            A.column = :value
    """,
    in_parameters={'value': 'This value'}      
)
mysql.close()

#####################################################################
# Connect to an Existing SQLite database
existing_sqlite = SQLite(
    filepath='path/to/existing/sqlite.db',
)
existing_sqlite.close()

#####################################################################
# Create a new, blank SQLite database
new_sqlite = SQLite(
    filepath='path/to/non-existent/sqlite.db',
)
new_sqlite.close()

#####################################################################
# Create a new in-memory SQLite database and save to disk
in_mem_sqlite = SQLiteInMemory()
in_mem_sqlite.query(
    sql="""
        CREATE TABLE my_table (
            [a_number]  INTEGER,
            [a_date]    DATETIME,
            [a_double]  DOUBLE,
            [a_string]  VARCHAR
        );
        """
)
in_mem_sqlite.commit()
in_mem_sqlite.insert_values(
    table_name='my_table',
    a_number=1,
    a_date=new_access.dt_now(),
    a_double=22.22,
    a_string='HelloWorld'
)
in_mem_sqlite.commit()
saved_sqlite = in_mem_sqlite.save_as(
    filepath='path/to/sqlite.db',
    return_new_database_manager=True
)
in_mem_sqlite.close()
dataframe = saved_sqlite.query('SELECT * FROM my_table')
saved_sqlite.close()

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

dbpd-1.0.tar.gz (10.9 MB view details)

Uploaded Source

Built Distribution

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

dbpd-1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file dbpd-1.0.tar.gz.

File metadata

  • Download URL: dbpd-1.0.tar.gz
  • Upload date:
  • Size: 10.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for dbpd-1.0.tar.gz
Algorithm Hash digest
SHA256 ffea916b05583d9c3dc3d1e3742380b39717896d375d17709f1a2fb85383b784
MD5 d45831c980adac84ddc13abfd7ecc8b9
BLAKE2b-256 937ead487723cff38af69cfa5233c892d50cf24c4315e8d71f542b258de75803

See more details on using hashes here.

File details

Details for the file dbpd-1.0-py3-none-any.whl.

File metadata

  • Download URL: dbpd-1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for dbpd-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 890a67204bcea72a1f4bf67ec74229727d4d1a60f66c538788a7c85aaca7a096
MD5 c1aa6d40723d8ce18a606aa2d4a51f00
BLAKE2b-256 167aa91cfa9a7e506a98a05d9411166d04c56aed9416127e9b5278c60c131270

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