Skip to main content

A small utility package that makes connecting to Microsoft SQL easier

Project description

Microsoft SQL Server Easy Connect

The purpose of this small utility, one class module is to make connecting to a MS SQL Server easier. One problem I experienced through putting python code in production is: the operating system makes a difference. So if one has a error-free project running on their Windows machine and then deploy it on a unix based server, errors appear. This modul is OS independant (with one caveat, see below) so that regardless of where you the project is deployed, it will run without errors.

Quick Start

$ pip install py_ms_sql

Usage

from py_ms_sql import py_ms_sql

# Set up connection object (OPTIONAL: pass a logger object)
sql_conn = py_ms_sql.ConnectSQL()

# Use one of the following (in order of ease-of-use)
# DSN of ODBC
state, msg = sql_conn.connect(dsn='mysqldsn', uid='user', pwd='password')

# OR
# ODBC Driver String
state, msg = sql_conn.connect(driver_string="{SQL Server};SERVER=server_name;" \
                               "DATABASE=my_db;UID=user;PWD=password")

# OR
# ODBC Driver Details
state, msg = sql_conn.connect(driver='{SQL Server}',server='server_name',
                 db='my_db',uid='user',pwd='password')

# OR
# Specify ODBC Driver Location
state, msg = sql_conn.connect(host="my-server.com",port=1433,db="my_db",
                 uid='user',pwd=password,tds_version=7.3
                 driver='/usr/local/lib/libtdsodbc.so')

# Run a SELECT statement query (return error status and Pandas DataFrame)
state, data = sql_conn.select(sql_query)

# OR
# Run an INSERT statement query (expects a table name and pandas DataFrame)
state, msg = sql_conn.insert_df(df, table)

# Close connection
state, msg = sql_conn.close_conn()

Run any one of the above commands and if the code can find the ODBC driver, it will connect.

NOTE: The .query() method returns a Pandas DataFrame

Self Debug

In the event of errors try the following:

  1. Check, double check and check your connection details one more time.
  2. If you're connecting from something other than a Microsoft Machine, you may need to use the last option since some MS Sql Server use instance name to have a discoverable port number, and non-Microsoft machines struggle to discover port number (sometimes)
  3. If using FreeTDS on a non-Microsoft machine, make sure it is installed properly

TODO

  • allow for writing to MS SQL Server (if incomplete, only allows for read queries)
  • improve ease of portability so that no effort (what-so-ever) is needed to accomodate operating system
  • allow for non-Pandas DataFrame returns

Fine Print

The main reason operating systems cause errors with connecting to MS SQL Server is due to the drivers and how a machine finds the drivers. For example, Windows can connect using an ODBC manager (so can MAC OSX, but not the easiest). Therefore, to make sure your deployment doesn't run with errors, use os.name to determine which OS is running and call the correct driver/use the appropriate connection details.

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

py_ms_sql-0.2.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

py_ms_sql-0.2.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file py_ms_sql-0.2.2.tar.gz.

File metadata

  • Download URL: py_ms_sql-0.2.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for py_ms_sql-0.2.2.tar.gz
Algorithm Hash digest
SHA256 c3fb3f5c6d1a28969b7d97e3021ca4fbb5f9f082cc8c6519e720e7045a39eabd
MD5 30b70184e4dcd4cd584e74cb4c06c299
BLAKE2b-256 4e3d527202f3e468c01ee4e41d6b75eff07079faac88e03723bbc6e8eae9a0f2

See more details on using hashes here.

File details

Details for the file py_ms_sql-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: py_ms_sql-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.1

File hashes

Hashes for py_ms_sql-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a2eabd03b2d6cb01ff0b3985b2c58b6be6ebc53a70b8d448c4712f0e0c03fac5
MD5 62019412cc43d7325113fadc0904a465
BLAKE2b-256 aad31f27300fa6c5f4aa0b73558e6468bc2179a8d1dac3e30f84722378f36a2e

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