Django-pydobc 在 Windows 上连接 SQL Server 出现问题

9
我和另一个开发人员正在使用遗留的SQL Server数据库(SQLEXPRESS)在另一台服务器上设置django项目(v1.4.2)。到目前为止,我们已经能够使用django-pyodbc从Linux和Mac连接到数据库,以及使用django-mssql从运行Windows 7的笔记本电脑连接到数据库。我想在笔记本电脑上使用django-pyodbc来保持环境同步。
在笔记本电脑上:
- 安装了pyodbc(3.0.6),并且在非django.py脚本中可以连接并运行sql语句 - 下载django-pyodbc 1.4,并通过下载zip文件进行安装。我不确定是否安装正确: - 我解压了文件,并在顶层目录下运行了setup.py文件;它会将sql_server目录放置在/lib/site-packages目录中 - 将此sql_server目录复制到/django/db/backends目录 - 创建PYTHONPATH环境变量,指向/django/db/backends/sql_server。不确定它是否应该指向/site-packages/sql_server? - 创建ODBC数据源(系统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
        }
    },
}

我不确定这是否能帮助解决问题,但是使用仅在Windows上运行的django-mssql(可用于IT技术),工作条目如下:

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'

4
如果“事后分析”可以回答你的问题,要么将其作为答案并接受它,要么删除该问题(这样它就不会作为未回答的问题存在)。 - meataxe
1个回答

3

尝试使用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系统中,OPTIONS中的'driver'条目应该是:
'driver': 'SQL Native Client',

编辑:糟糕,没有看到您已解决了问题。保留我的答案作为参考。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接