Skip to main content

A set of tools to simplify connecting to and working with an ODBC data source

Project description

Overview

odbc tools is a set of tools to simplify connecting to and working with an ODBC data source. It includes a context manager for handling connections to the data source and the ability to return a result set in an easy to use format or write the results to a file.

Install It

From PyPI

$ pip install odbctools

Use It

Odbctools by default uses a file called config.ini, or you can override this for different scenarios, including passing the dsn directly.

Create config.ini

[odbc]
DSN=SourceName

Code Example

from odbctools import OdbcManager

query_string = "Your query here"
params = [p1, p2]

with OdbcManager() as datasource:
    result_list = datasource.get_dictionaries(query_string, params)
    for result_dict in result_list:
        for col_name, col_data in result_dict.items():
            print("{0}{1}".format(str(col_name).ljust(25), col_data))

Another Example

from odbctools import OdbcManager

query_string = "Your query here"
params = [p1, p2]

with OdbcManager(dsn='ODBC Name Here') as datasource:
    result_list = datasource.get_dictionaries_in_queue(query_string, params)
    for result_dict in result_list:
        for col_name, col_data in result_dict.items():
            print("{0}{1}".format(str(col_name).ljust(25), col_data))

Encapsulating the connection in your classes is also possible

class Foo:
    def __init__(self):
        self.connection = OdbcManager(dsn='unity64')

    def __enter__(self):
        self.connection.open_connection()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.connection.close_connection()

with Foo() as f:
    data = f.connection.get_dictionaries('select top 1 * from people', list())

Note: You can now create a OdbcManager object with a connection string, passing in the connection_string argument.

Returning data from a query

query - Returns list that contains [0]A list containing the query columns [1:] A list of the resulting data (rows)

get_dictionaries - Queries data source, returns a list of dictionaries.

get_dictionaries_in_queue - Queries data source, returns a deque of dictionaries.

write_to_csv - Queries data source and writes results to a CSV file

Updating data

Note: by default commit is turned off, if you want to enable auto commit, you can set it when creating the OdbcManager object or when calling this function.

query_no_resultset - For running DDL/DML queries for creating, deleting, updating records.

Dependencies

  • pypyodbc

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

odbctools-0.7.0.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

odbctools-0.7.0-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file odbctools-0.7.0.tar.gz.

File metadata

  • Download URL: odbctools-0.7.0.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for odbctools-0.7.0.tar.gz
Algorithm Hash digest
SHA256 ee1585b7cd0f4b8b639b0acd713d813c1291b23f249fd4459511acc67bf7ad44
MD5 8b25c6c8fadc663e99bb713d723222be
BLAKE2b-256 7c3772cc75204b784b988f99a585571bdd1845d75cbf1413a16c46764602de51

See more details on using hashes here.

File details

Details for the file odbctools-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: odbctools-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for odbctools-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f626011fcc8ca49b19c274bf54ef17752fe147eef0d7fb598470f0a1e0a01474
MD5 b7a8fd8819b01498f75acfc24a1a40fd
BLAKE2b-256 c3bc1872b93b1b7c0e1bacee5b58e0815e9f71c25dd2ea68c5511b113b0130a2

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page