Created
July 24, 2018 11:32
-
-
Save wwex/0d37af512a525f8d24b37edb68229c41 to your computer and use it in GitHub Desktop.
PyMemos - SQL queries #SQL #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyodbc | |
import pandas as pd | |
SQL_CONFIG = r"DRIVER={SQL Server}; SERVER=adres\serwera; DATABASE=nazwa_bazy; UID=user_id; PWD=password" | |
def connect(): | |
try: | |
conn = pyodbc.connect( | |
SQL_CONFIG, | |
autocommit=True | |
) | |
cursor = conn.cursor() | |
return cursor | |
except Exception as err: | |
print(err) | |
def sql_query(cursor, query): | |
try: | |
query_execute = cursor.execute(query) | |
query_execute = query_execute.fetchall() | |
return query_execute | |
except Exception as err: | |
print(err) | |
def get(query): | |
try: | |
cursor = connect() | |
result = sql_query(cursor, query) | |
return result | |
except Exception as err: | |
print(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment