Accessing SQL Server from Python

It’s really not hard to access a SQL Server database from python, but you have to make sure everything is set up correctly.

First, make sure that the database server is set up for “Windows and SQL Server Authentication.” This can be set in the server security properties (in SQL Server Management Studio = SSMS, right click the server and go to Properties > Security). I spent quite a while pulling my hair out about failed logins, when this was the issue. After changing this setting, you’ll need to restart the server (using the Windows services administrative panel).

Then, within SSMS create a login, and a user for each database connected to that login, and make sure the user has been GRANTed access to the database (giving it the owner role is the easiest way to do that).

Now you should be able to login from python. Using adodbapi:

>>> adodbapi.connect(“Provider=SQLOLEDB; Data Source=.\SQLEXPRESS; Initial Catalog=database_name; User Id=user_id; Password=user_password”)

 

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: