cjmarques
cjmarques

Reputation: 672

Web2Py won't connect to MSSQL

I'm unable to get web2py to connect to mssql.

<type 'exceptions.RuntimeError'>(Failure to connect, tried 5 times:
'NoneType' object has no attribute 'connect')

My connection string is:

db = DAL('mssql://testUser:password1@localhost/testDB') 

Environment
  Windows Server 2008 R2, 64-bit operating system
  SQL Server 2008 R2, local.
  Web2py: source code install version 1.99.2 (2011-09-26 06:55:33) stable.
  pyodbc
  Python 2.7.2

I've tested that I can connect using the pyodbc. The following code works:

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testDB;UID=testUser;PWD=password1')
cursor = cnxn.cursor()
cursor.execute("select * from tbUsers")
rows = cursor.fetchall()
for row in rows:
  print row

Thanks for your time.

Corey.

Upvotes: 4

Views: 3545

Answers (2)

ttemple
ttemple

Reputation: 1995

After confirming that your login is correct and that you have pyodbc installed, be sure that the connection string for the server is as follows if your db server name has a backslash in it (e.g. localhost\dbServerName):

    db = DAL('mssql://testUser:password@localhost\dbServerName/testDB')

You can substitute the localhost with an IP Address as well.

Environment:
Windows 7 Professional, 32-bit operating system
SQL Server 2008 R2 connecting over a network
Web2py: source code install version 2.4.6 stable
pyodbc: pyodbc-3.0.5.win32-py2.7.exe
Python 2.7.3

Upvotes: 0

cjmarques
cjmarques

Reputation: 672

I've just received a solution from Massimo Di Pierro on the Web2Py forum. He deduced the cause and provided a work around.

Not sure if the "import pyodbc" is needed. Once the driver was assigned it stayed, even after a restart of the server.

# Test if the mssql driver is assigned. Sets it up if it isn't.
import pyodbc
from gluon.dal import MSSQLAdapter
if not (MSSQLAdapter.driver):
  MSSQLAdapter.driver = globals().get('pyodbc',None)

db = DAL('mssql://testUser:password@localhost/testDB')

Upvotes: 2

Related Questions