Windows 上的 Django-pyodbc SQL 服务器连接问题

2023-12-23

我和另一位开发人员正在另一台服务器上使用旧版 SQL Server 数据库 (SQLEXPRESS) 设置 django (v1.4.2) 项目。到目前为止,我们已经能够使用 django-pyodbc 从 Linux 和 Mac 连接到数据库,并使用 django-mssql 从运行 Windows 7 的笔记本电脑连接到数据库。我想在笔记本电脑上使用 django-pyodbc 来保持环境同步。

在笔记本电脑上:

  • pyodbc (3.0.6) 已安装,并且在非 django .py 脚本中我可以连接并运行 sql 语句
  • Downloaded django-pyodbc 1.4 by downloading the zip; I'm not sure I installed it right:
    • 我解压该文件,并运行顶层目录中的 setup.py 文件;它将 sql_server 目录放入 /lib/site-packages 目录中
  • 将此 sql_server 目录复制到 /django/db/backends
  • Created a PYTHONPATH environment variable pointing to /django/db/backends/sql_server
    • 不确定它是否应该指向 /site-packages/sql_server ?
  • Created an ODBC Data Source (System DSN)
    • 测试连接选项是否有效
  • 编辑了 settings.py 中的 DATABASE 条目,使其几乎与 Linux 版本完全相同(详细信息如下)

所以,它不起作用,我收到以下错误消息,并且不知道下一步该怎么做:

('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

我设置 django settings.py 文件如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sql_server.pyodbc',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'something_else',
        'HOST': 'mssqlx',
        'PORT': '12345',
        'OPTIONS': {
            'driver': 'SQL Server',
        },
    },
}

在 Linux 上,我的设置文件有一个 DATABASES 条目,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sql_server.pyodbc',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'something_else',
        'HOST': 'mssqlx',       # ODBC DSN defined in /etc/freetds.conf
        'PORT': '12345',        # Probably unneeded.  Set in mssqlx
        'OPTIONS': {
            'driver': 'SQL Server',  # ODBC driver name in /etc/odbcinst.ini
            'extra_params': "TDS_VERSION=7.0"  # Probably unneeded.  Set in mssqlx
        }
    },
}

不知道它是否有助于解决这个问题,但是使用 django-mssql (仅在 Windows 上运行),(工作)条目是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlserver_ado',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'something_else',
        'HOST': '199.555.0.10',         # changed for this example
        'PORT': '12345',
        'OPTIONS': {'provider': 'SQLOLEDB'}
    },
}

不知道其他信息可能有帮助。感谢您提供的任何帮助或见解。

----验尸 ---- 这是最终起作用的:

数据库设置中的部分条目:

    'default': {
        'ENGINE'    : 'django.db.backends.sql_server.pyodbc',
        'NAME'      : 'test_db_name',
        'USER'      : 'test_db_user_name',
        'PASSWORD'  : 'password',
        # ODBC DSN defined in /etc/freetds.conf
        'HOST'      : 'mssql_test',
        # Ignored for Windows; Required for Linux
        'OPTIONS'   : {
            # ODBC driver name in /etc/odbcinst.ini
            'driver': 'SQL Server',
            # NOTE: dsn option is added dynamically later, for Windows
        }
    },

# The ODBC DSN name specified above as DATABASES.default.HOST is ignored on
# Windows, where it must be specified as DATABASES.default.OPTIONS.dsn instead.
# However, we haven't found a way to make DATABASES.default.OPTIONS.dsn work in
# Linux (and probably the same for Mac).  It causes the error:
#    Data source name not found, and no default driver specified 
# Therefore we add it here, but only for Windows.
# Note: The username and pwd in the windows dsn file is apparently NOT used
#       (b/c server hosts both test and prod database in same MSSQL
#       instance, both test and prod dsn files happen to work - they have the
#       same ip address and port number, but different username/password's)
#
# On 64-bit Windows, with our current 32-bit version of pyodbc, the DSN
# must be created via:
#    C:\Windows\SysWOW64\odbcad32.exe
# instead of the regular "ODBC Data Sources" app in Control Panel, which 
# invokes:
#    C:\Windows\system32\odbcad32.exe
#
#   os.name is...
#       nt      for Hans' laptop (Windows 7)
#       posix   for the "Amazon Linux AMI" (CentOS) on AWS
#       posix   for Fred's Mac
if os.name == 'nt':      # Windows
    DATABASES['cf']['OPTIONS']['dsn'] = 'mssql_test'

尝试使用https://github.com/michiya/django-pyodbc-azure https://github.com/michiya/django-pyodbc-azure。这应该适用于 Linux 和 Windows。

然后定义您的数据库设置,如下所示:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'dbname',
        'HOST': 'dsn_entry',
        'PORT': 'port',
        'USER': '',
        'PASSWORD': 'pass',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'dsn': 'dsn_entry',
            'host_is_server': True
        }
    }
}

在 Windows 下'driver'进入OPTIONS应该:

'driver': 'SQL Native Client',

编辑:哎呀,没看到你已经解决了问题。留下我的答案作为参考。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows 上的 Django-pyodbc SQL 服务器连接问题 的相关文章

随机推荐