Connecting to MS SQL — Anaconda documentation (2024)

Anaconda Enterprise enables you to easily connect to an Microsoft SQL server database, to access the data stored in it.

Before you can do so, however, you’ll need to install the pymssql conda package which contains a simple database interface for Python to work with MS SQL Server:

NOTE: Any packages you install from the command line are available during the current session only. If you want them to persist, add them to the project’s anaconda-project.yml file. For more information, see Developing a project.

You can then use code such as this to connect to the MS SQL database from within a notebook session:

import pymssqlimport configparser"""Setup config parser and read Kubernetes secret .ini style credentials file. For example:[default]username=USERNAMEpassword=PASSWORD"""config = configparser.ConfigParser()config.read('/var/run/secrets/user_credentials/mssql_credentials')# Setup URI and database to useserver = 'example-mssql.dev.anaconda.com'database = 'SampleDB'# Define the connection using variables pulled from secretconnection = pymssql.connect( server, config.get('default', 'username'), config.get('default', 'password'), database)# Setup the cursor and execute an example querycursor = connection.cursor()cursor.execute("""SELECT TOP (10) [AddressID] ,[AddressLine1] ,[AddressLine2] ,[City] ,[StateProvinceID] ,[PostalCode] ,[SpatialLocation] ,[rowguid] ,[ModifiedDate]FROM [AdventureWorks2016].[Person].[Address]""")# Print the results from the queryrow = cursor.fetchone()while row: print(row) row = cursor.fetchone()# Close the connection once completeconnection.close()

See Storing secrets for information about adding credentials to the platform, to make them available in your projects. Any secrets you add will be available across all sessions and deployments associated with your user account.

Connecting to MS SQL — Anaconda documentation (2024)
Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 6084

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.