Skip to content

Instantly share code, notes, and snippets.

@wwex
Created July 24, 2018 11:32
Show Gist options
  • Save wwex/0d37af512a525f8d24b37edb68229c41 to your computer and use it in GitHub Desktop.
Save wwex/0d37af512a525f8d24b37edb68229c41 to your computer and use it in GitHub Desktop.
PyMemos - SQL queries #SQL #python
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