___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Query MSSQL DB from CentOS and Windows using Python's pyodbc Library

BY: @rohancmr | CREATED: Jan. 17, 2018, 6:56 p.m. | VOTES: 10 | PAYOUT: $32.30 | [ VOTE ]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516212573/hrtl10t72x3p5zdfvu0h.jpg]

This tutorial explains how we can connect to a Microsoft SQL Server database from Python running under Linux or Windows using pyodbc library.

Pyodbc is an open source Python module that makes accessing ODBC databases simple. It implements the DB API 2.0 specification. Using it, we can easily connect Python applications to data sources with an ODBC driver.

What this tutorial covers ?

Requirements

Difficulty

The implementation explained here is straightforward.

SQL Instance

In this tutorial, we'll use Steem SQL instance. Details are mentioned below :

SQL Server Setup

Python Library to connect to MSSQL DB

Installation of pyodbc

The easiest method to install pyodbc is by using pip. Use below command to install pyodbc library.

pip install pyodbc

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516212416/dl1zqegx4n1qel0295kf.gif]

Installation under CentOS or RedHat

Required packages under CentOS

Installation Steps

rpm -Uvh http://mirror.vbctv.in/epel//7/x86_64/e/epel-release-7-9.noarch.rpm

yum install -y unixODBC unixODBC-devel freetds

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213051/aerzizq9qp1l0tbokm6v.gif]

We can connect to SQL server with or without Data Source Name (DSN). Both the methods are mentioned below.

Method 1 : Configure using Data Source Name (DSN)

Configure MSSQL Server in FreeTDS's setting :

[steemsqlserver]
host = sql.steemsql.com
port = 1433
tds version = 7.0

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213129/qbnxp08qgb1ol2nx5izx.gif]

tsql -S steemsqlserver -U steemit -P steemit

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213182/utvztjpi8i2ccholrw3d.gif]

If everything works fine, it will print the result of the query.

Setup unixODBC to use FreeTSD

odbcinst_j.PNG

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213210/fwea53ybystcom2etta9.gif]!

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213300/k8daaod0kecqyosqvpn8.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213345/yozlqzopulzvikm9sros.gif]

If the Driver and Setup does not work then search the libtds files using find command.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213378/fyftle2n7clxewf2eyik.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213408/vupfeftfpnablsrvzjp2.gif]

Connect from Python Application

pyodbc.connect('DSN='steemsqlserverdatasource'; UID='steemit'; PWD=''steemit; DATABASE='DBSteem';'

Example : Below code connects to the Steem SQL server and list all accounts which starts with 'rohan'.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213452/og6hkpzxi6rmjshcwsjq.png]

Code Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213526/ydbyfmd24mg2eyua0k9m.gif]

Method 2 : Configure without Data Source Name.

In this case, we specify the driver, SQL server name and ports and TDS Version in the pyodbc connection string.

pyodbc.connect('Driver='FreeTDS'; Server='sql.steemsql.com'; UID='steemit'; PWD='steemit'; Database='DBSteem'; TDS_Version='7.0'; Port='1433';')

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213555/bh0p12iqx4lxwvfdyaty.png]

Code Output

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213613/pp5pbcojhdsgfde8pihx.gif]

Installation under Windows

https://www.microsoft.com/en-us/download/details.aspx?id=53339

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213671/huhnm5rbfzqyowkew5ge.gif]

pyodbc.connect('Driver={ODBC Driver 13 for SQL Server}; Server=sql.steemsql.com; UID=steemit; PWD=steemit; Database=DBSteem;')

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213767/bsm1t8htbeec1hy6q8gx.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1516213808/ihglfi1ssalvxicoef4y.gif]

We saw how easily we can connect to Microsoft SQL server from CentOS or Windows using Python's pyodbc library.

If you have any questions or comments , I'd would love to hear from you in comment section.

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #steemsql ] [ #pyodbc ] [ #sql ] [ #odbc ]

Replies

@fuzeh | Jan. 17, 2018, 10:27 p.m. | Votes: 1 | [ VOTE ]

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

@rohancmr | Jan. 18, 2018, 2:55 a.m. | Votes: 0 | [ VOTE ]

Thankyou.

@rohancmr | Jan. 18, 2018, 2:55 a.m. | Votes: 0 | [ VOTE ]

@originalworks

@originalworks | Jan. 18, 2018, 3:08 a.m. | Votes: 0 | [ VOTE ]

The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!
[IMAGE: https://steemitimages.com/DQmaBi37A5oTnQ9NBLH8YU4jpvhhmFauyvgg3YRrEJwskM9/ezgif.com-resize.gif]

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

@utopian-io | Jan. 18, 2018, 3:53 p.m. | Votes: 0 | [ VOTE ]

Hey @rohancmr I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
- Vote for my Witness With SteemConnect
- Proxy vote to Utopian Witness with SteemConnect
- Or vote/proxy on Steemit Witnesses

[IMAGE: https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif]

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>