Web2Py 无法连接到 MSSQL

发布于 2024-12-11 19:59:13 字数 768 浏览 0 评论 0原文

我无法让 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

感谢您的宝贵时间。

科里.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-12-18 19:59:13

我刚刚在 Web2Py 论坛上收到 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')

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')
没有你我更好 2024-12-18 19:59:13

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

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

您也可以用 IP 地址替换本地主机。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文