Web2Py将无法连接到MSSQL

时间:2011-10-25 15:03:15

标签: sql-server-2008 web2py

我无法让web2py连接到mssql。

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

我的连接字符串是:

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

环境
Windows Server 2008 R2,64位操作系统
SQL Server 2008 R2,本地。
Web2py:源代码安装版本1.99.2(2011-09-26 06:55:33)稳定 pyodbc
Python 2.7.2

我已经测试过我可以使用pyodbc进行连接。以下代码有效:

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

感谢您的时间。

科里。

2 个答案:

答案 0 :(得分:1)

我刚收到了来自Web2Py forum的Massimo Di Pierro的解决方案。他推断出原因并提供了解决方法。

不确定是否需要“导入pyodbc”。一旦分配了驱动程序,即使重新启动服务器,它仍然存在。

# 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')

答案 1 :(得分:0)

确认您的登录正确并且安装了pyodbc后,如果您的数据库服务器名称中有反斜杠,请确保服务器的连接字符串如下(例如 localhost \ dbServerName ):

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

您也可以使用IP地址替换localhost。

环境:
  Windows 7专业版,32位操作系统
  SQL Server 2008 R2通过网络连接
  Web2py:源代码安装版本2.4.6稳定
  pyodbc:pyodbc-3.0.5.win32-py2.7.exe
  Python 2.7.3

相关问题