1

I started using a lot of sql using ms sql server, and would like to automate some steps using python script.

My plan is to have the python using various inputs so that if and when I need to get different output from sql server, I can just run the python script and execute the SQL code in the Microsoft SQL Server Management Studio. This way, I can get the results I wanted quickly without manually tweeking the sql code all the time for my different needs (but using the same code).

So far I've checked out all (but not limited to) these posts:

  1. Python Pycharm and SQL Server connection

  2. https://www.jetbrains.com/help/pycharm/2016.3/databases-and-sql.html

  3. https://intellij-support.jetbrains.com/hc/en-us/community/posts/205816489-New-to-Python-Help-with-SQL-Server-Connection-?sort_by=votes

However, I have not been able to fully understand how to connect them. Or maybe I am understanding it wrong, it's actually not connecting the two? I could not find much information about this topic. Any help is greatly appreciated!

Note:

to clarify, i dont even know how to start :( So ANY guidance is really appreciated!

Community
  • 1
  • 1
alwaysaskingquestions
  • 1,595
  • 5
  • 22
  • 49
  • please state where do you get stuck. – Marshall X Apr 04 '17 at 00:16
  • Hi @MarshallX, just updated my question accordingly as well. I dont even know where to start... am quite clueless as I am still beginner in sql. – alwaysaskingquestions Apr 04 '17 at 00:18
  • @alwaysaskingquestions, did you download/install [MySQL-python](https://pypi.python.org/pypi/MySQL-python) and import it in your python script? Checkout this post too [How do I connect to a MySQL Database in Python?](http://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python), which looks like a complete walkthrough – chickity china chinese chicken Apr 04 '17 at 00:22
  • Look into [`pyodbc`](https://github.com/mkleehammer/pyodbc) module to connect to SQL Server to run DDL and DML statements. Plenty of examples here on SO. – Parfait Apr 04 '17 at 02:52
  • @Parfait thank you so much! i did look into it and it worked for me :) – alwaysaskingquestions Apr 04 '17 at 19:53
  • This tutorial can also help: https://blog.jetbrains.com/datagrip/2016/06/21/connecting-datagrip-to-ms-sql-server/ – moscas Mar 15 '19 at 11:17

1 Answers1

2

Have you installed a ms sql driver for python, yet? If you never have been working with python and databases then that could be the most common mistake.

If not, get the driver first from here

https://learn.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server

after installing the driver just connect to your database as shown in the tutorials in the link above.

dima
  • 158
  • 3
  • 14
  • 1
    so this is not a library which i can just install in python right? i have to download this and install like an application? thanks so much by the way! and you are right, i am completely new about this. – alwaysaskingquestions Apr 04 '17 at 05:59