xoxomos
asked on
Python Connect To SQLServer Express
On windows 7 running python 3.4. Trying to connect to SQLServer Express with the connection:
conn = pymssql.connect('SQLEXPRES S', 'sa', 'master', 'tempdb')
C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 632, in _mssql.MSSQLConnection.__i nit__ (_mssql.c:6169
)
_mssql.MSSQLDriverExceptio n: Connection to the database failed for an unknown re
ason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect('SQLEXPRES S', 'sa', 'master', 'tempdb')
File "pymssql.pyx", line 639, in pymssql.connect (pymssql.c:10246)
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
conn = pymssql.connect('SQLEXPRES
C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 632, in _mssql.MSSQLConnection.__i
)
_mssql.MSSQLDriverExceptio
ason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect('SQLEXPRES
File "pymssql.pyx", line 639, in pymssql.connect (pymssql.c:10246)
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
According to this page http://pymssql.sourceforge.net/examples_pymssql.php , you are missing quite a bit in your connect statement at the very least. This http://pymssql.org/en/latest/ appears to be the current page.
ASKER
from os import getenv
import pymssql
server = getenv("PYMSSQL_TEST_SERVE R")
user = getenv("PYMSSQL_TEST_USERN AME")
password = getenv("PYMSSQL_TEST_PASSW ORD")
#conn = pymssql.connect(server, user, password, "tempdb")
conn = pymssql.connect(server="SQ LEXPRESS", user="sa", password="master", database="tempdb")
cursor = conn.cursor()
..
.
.C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 632, in _mssql.MSSQLConnection.__i nit__ (_mssql.c:6169
)
_mssql.MSSQLDriverExceptio n: Connection to the database failed for an unknown re
ason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect(server="SQ LEXPRESS", user="sa", password="master", da
tabase="tempdb")
File "pymssql.pyx", line 639, in pymssql.connect (pymssql.c:10246)
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
That freetds generated a directory of it's own c:\freetds-0.95rc2.
Is it necessary for it's bin directory to be in PATH?
import pymssql
server = getenv("PYMSSQL_TEST_SERVE
user = getenv("PYMSSQL_TEST_USERN
password = getenv("PYMSSQL_TEST_PASSW
#conn = pymssql.connect(server, user, password, "tempdb")
conn = pymssql.connect(server="SQ
cursor = conn.cursor()
..
.
.C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 632, in _mssql.MSSQLConnection.__i
)
_mssql.MSSQLDriverExceptio
ason.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect(server="SQ
tabase="tempdb")
File "pymssql.pyx", line 639, in pymssql.connect (pymssql.c:10246)
pymssql.InterfaceError: Connection to the database failed for an unknown reason.
That freetds generated a directory of it's own c:\freetds-0.95rc2.
Is it necessary for it's bin directory to be in PATH?
ASKER
Changed the server from SQLEXPRESS to localhost. Getting slightly different errors.
C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 631, in _mssql.MSSQLConnection.__i nit__ (_mssql.c:6156
)
File "_mssql.pyx", line 1612, in _mssql.maybe_raise_MSSQLDa tabaseExce ption (_m
ssql.c:16426)
_mssql.MSSQLDatabaseExcept ion: (20009, b'DB-Lib error message 20009, severity 9:
\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib e
rror during Unknown error (10035)\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect(server="lo calhost", user="sa", password="master", dat
abase="tempdb")
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUna
ble to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error
during Unknown error (10035)\n')
C:\Python34>
C:\Python34>python sqlserver.py
Traceback (most recent call last):
File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097)
File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477)
File "_mssql.pyx", line 631, in _mssql.MSSQLConnection.__i
)
File "_mssql.pyx", line 1612, in _mssql.maybe_raise_MSSQLDa
ssql.c:16426)
_mssql.MSSQLDatabaseExcept
\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib e
rror during Unknown error (10035)\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sqlserver.py", line 7, in <module>
conn = pymssql.connect(server="lo
abase="tempdb")
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUna
ble to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error
during Unknown error (10035)\n')
C:\Python34>
How did you get FreeTDS to rub under Windows? Usually, you have to install SQL Native Client for your version of SQL Server Express. The advantage of that is that you can test it in the ODBC manager to make sure your connections and credentials are working.
ASKER
SQL Native Client was just there. I'm sure I did not do anything to install it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In this case the machine is local, the desktop. Yes, the ODBC manager says there is a successful connection.
ASKER
Looking in the module
pymssql.pyx line 573
try:
conn = _mssql.connect(server, user, password, charset, database, appname, port)
Here there are seven parameters to be passes
however the example shows only four parameters
conn = pymssql.connect(host='134. 154.228.21 7', user='sa', password='master', database='master')
pymssql.pyx line 573
try:
conn = _mssql.connect(server, user, password, charset, database, appname, port)
Here there are seven parameters to be passes
however the example shows only four parameters
conn = pymssql.connect(host='134.
ASKER
Had to get sqlserver listening on 1433 instead of some number fifty-thousand port.