Skip to main content

Easy Python database interaction

Project description

easy_db

easy_db is a high-level Python library designed to simplify working with databases. The "DataBase" class handles connecting to various types of databases while providing simple methods for common tasks. The underlying database connection and cursor can be used when more precise control is desired.

Goals

  • Make common database tasks simple and easy
  • Intelligently handle different database types
  • Provide intuitive, consistent, Pythonic methods database interaction
  • Provide good performance without requiring polished query code
  • Expose database connection and cursor to users wanting fine-grained control
  • Just get the data into Python so we can use it!

Why use easy_db?

Before easy_db:

import pyodbc
import os

conn = pyodbc.connect(
    r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};' +
    r'DBQ=' + os.path.abspath('MyDatabase.accdb') + ';')
cursor = conn.cursor()
cursor.execute('SELECT * FROM test_table;')
data = cursor.fetchall()
columns = [col[0] for col in cursor.description]
table_data = [dict(zip(columns, row)) for row in data]

# table_data -> [{'column1': value1, 'column2': value2}, {...}, ...]

Using easy_db:

import easy_db

db = easy_db.DataBase('MyDatabase.accdb')
table_data = db.pull('test_table')

# table_data -> [{'column1': value1, 'column2': value2}, {...}, ...]

Quick Start

Let's first connect to a SQLite database.

import easy_db
db = easy_db.DataBase('test_sqlite3_db.db')

Now let's see what tables are available in this database.

tables = db.table_names()

Table columns and types are simple to investigate.

print(db.columns_and_types('example_table'))

Let's pull all of the data from a table. We could start with something like "SELECT * ...", but this is way more fun:

data = db.pull('example_table')

Note that the table/query data is returned as a list of dictionaries with column names as dictionary keys.

  • Pro Tip: If desired, a Pandas dataframe of the same form as the database table can be easily created from this data structure using:
import pandas
df = pandas.DataFrame(data)

Now perhaps we have an Access database and would like to pull in a table from our SQLite database. easy_db makes this simple and gracefully handles the nuances of dealing with the different databases.

db = easy_db.DataBase('test_sqlite3_db.db')
db_2 = easy_db.DataBase('test_access_db.accdb')

db_2.copy_table(db, 'example_table')

The DataBase object can be used as a context manager for running custom SQL. The cursor is provided and the connection runs .commit() and .close() implicitly after the "while" block.

with db as cursor:
    cursor.execute('DELETE * FROM example_table;')

easy_db.DataBase Methods

  • Connect to the database...
db = easy_db.DataBase(...)

Pulling Data

db.pull('tablename')
db.pull_where('tablename', 'sql_condition')
db.pull_where_id_in_list('tablename', 'id_column', match_values_list)

Updating Data

db.append('tablename', new_table_rows)  # new_table_rows is a list of dicts
db.update('tablename', 'match_column', 'match_value', 'update_column', 'update_value')
db.delete_duplicates('tablename')

Database Info

db.table_names()
db.query_names()  # for Access
db.columns_and_types('tablename')
db.key_columns('tablename')
db.size  # property with size of database in GB
db.compact_db  # compact & repair Access db or vacuum SQLite db

Table Manipulation

db.create_table('tablename', columns_and_types)
db.drop_table('tablename')
db.copy_table(other_db_with_tablename, 'tablename')
db.add_column('tablename', 'column')
db.drop_column('tablename', 'column')
db.create_index('tablename', 'column')

Custom Control

  • Context manager handles opening, commiting, and closing connection
with db as cursor:
    cursor.execute('SELECT * FROM tablename;')  # execute any SQL statement
  • Can also run .execute() on the database itself (shortcut for the above)
db.execute('SELECT * FROM tablename;')

Thanks for checking out easy_db!

License

MIT

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

easy_db-0.9.11.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

easy_db-0.9.11-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file easy_db-0.9.11.tar.gz.

File metadata

  • Download URL: easy_db-0.9.11.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/60.0.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.0

File hashes

Hashes for easy_db-0.9.11.tar.gz
Algorithm Hash digest
SHA256 e7f6920faa1a318c288bbb7e4b3351f008ef815d51e3419c16e8cdf5ba10030c
MD5 4106ab6b47b3aa727249342d845eb342
BLAKE2b-256 dd51af2070b61c09ba8e56136c03d38442de67a03fdea43fb2f560034aba77b1

See more details on using hashes here.

File details

Details for the file easy_db-0.9.11-py3-none-any.whl.

File metadata

  • Download URL: easy_db-0.9.11-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/60.0.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.0

File hashes

Hashes for easy_db-0.9.11-py3-none-any.whl
Algorithm Hash digest
SHA256 289376cdd28526a0c5197fc6943d590f13d58d3b46384ee9184d1de444f81bbc
MD5 c8e0cc3b3e627d95732986e9ca1af429
BLAKE2b-256 6239fdd3ac1e91ea30bc7be19399e5602e91dd936435a2a7a7f6f2c0e146a31e

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