Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Last active March 3, 2023 20:32
Show Gist options
  • Save jamescalam/1a13bee1d81439ca4b2511d7e9370f53 to your computer and use it in GitHub Desktop.
Save jamescalam/1a13bee1d81439ca4b2511d7e9370f53 to your computer and use it in GitHub Desktop.
import pyodbc
from datetime import datetime
class Sql:
"""Class used for establishing a Python to Microsoft SQL Server connection
and import/export/manipulation of data files inside the server.
"""
def __init__(self, database, server="XXVIR00012,55000"):
"""Here we are initialising our database and server parameters and
our connection to SQL server.
Parameters
----------
database: The SQL database name, eg 'database123'.
server: The SQL server, defaults to 'XXVIR00012,55000'.
"""
# database (eg database123) and server parameters
self.database = database
self.server = server
# here we are telling python what to connect to (our SQL Server)
self.cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server="+self.server+";"
"Database="+self.database+";"
"Trusted_Connection=yes;")
print("Setup connection for {} on {} server.".format(self.database,
self.server))
# initialise query attribute
self.query = "-- {}\n\n-- Made in Python".format(datetime.now()
.strftime("%d/%m/%Y"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment