3

I've been connecting to a MSSQL SERVER 2008 database using Microsoft SQL Server Management Studio graphic interface, as depicted in this screenshot

I would like to connect to this database using a python script. I installed pymssql and I've failed to connect to the database. I tried the following command:

import _mssql
conn = _mssql.connect(server="POLIVEIRA-PC\\MSSQLSERVER2008", user="POliveira-PC\\POliveira", password="my_password", database="database_name")

with and without the user and password flags. I always end up with this error:

Traceback (most recent call last): File "", line 1, in File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477) File "_mssql.pyx", line 632, in _mssql.MSSQLConnection.init (_mssql.c:6169) _mssql.MSSQLDriverException: Connection to the database failed for an unknown reason.

Can you help me connect to this database using Python (either using pymssql module or not). I'm not experienced with Python, nor do I with SQL, therefore I would like to do it in the simplest manner possible.

I'm running Windows 7 64 bit. Pyhton v2.7.9

3
  • In your screenshot you're using windows authentication, while your code is using SQLserver authentication... Is you server set up for this? Commented Jan 25, 2015 at 1:26
  • @thebjorn I don't know if the server is set up for that, but I would like my code to connect using windows authentication then. Let me just give you the context: I'm writing my MSc thesis and I was given a bunch of database files to extract data from. I'm the only one accessing the database, which is stored in my PC. Commented Jan 25, 2015 at 10:44
  • I don't think pymssql allows you to connect using windows authentication anymore (looks like it was removed in 2010 some time). If you really want to use windows authentication then the adodb library can do that. Commented Jan 25, 2015 at 15:50

2 Answers 2

1

I recommend you to use pyodbc, if you're using anaconda, use the 3.0.10 version of pyodbc, example:

import pyodbc 
from urllib.parse import quote_plus    
params = quote_plus("DRIVER={SQL Server};SERVER=POLIVEIRA-PC\\MSSQLSERVER2008;DATABASE=dbname;UID=userid;PWD=password")
    try: cnxn = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
    except Exception as e:
        raise SystemExit('Error: Conexion Base de Datos SQL %s' % e)

And if the problem is a remote connection in this links they talk about it

https://blogs.msdn.microsoft.com/walzenbach/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008/

http://akawn.com/blog/2012/01/configuring-sql-server-2008-r2-express-edition-for-remote-access/

Hope this works

Sign up to request clarification or add additional context in comments.

Comments

0

You need to create a login with SQL Server authentication and then connect with this user:

enter image description here

(you'll need to connect this login to a user etc., but that's unrelated to logging in), then use

import pymssql
pymssql.connect(host=r"POLIVEIRA-PC\MSSQLSERVER2008", user='logintest', password='secret', database='database_name')

don't use the _mssql module directly.

4 Comments

I've created a new login connection and then, when I tested that login via SQL Server Management Studio interface, I got this error
When trying to connect using pymssql, I got a similar error as before: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pymssql.pyx", line 639, in pymssql.connect (pymssql.c:10246) pymssql.InterfaceError: Connection to the database failed for an unknown reason.
That is a SQL Server setup issue (you need to configure your sql server to accept connections over tcp/ip).
let me thank you for trying to help me first of all. However, I did configure MS SQL Server to accept connections over tcp/ip, as described here and I'm still unable to perform a successful connection to the database using the newly created user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.