9

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

在笔记本电脑上:

  • pyodbc (3.0.6) 已安装并且在非 django .py 脚本中我可以连接并运行 sql 语句
  • 通过下载zip下载django-pyodbc 1.4;我不确定我是否安装正确:
    • 我解压缩了文件,并在顶层目录中运行了 setup.py 文件;它将 sql_server 目录放在 /lib/site-packages 目录中
  • 将此 sql_server 目录复制到 /django/db/backends
  • 创建了一个指向 /django/db/backends/sql_server 的 PYTHONPATH 环境变量
    • 不确定它是否应该指向 /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
        }
    },
}

不知道它是否有助于解决这个问题,但是使用 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'}
    },
}

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

----POST MORTEM ----这是最终奏效的:

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

    '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

1 回答 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 下,'driver'输入OPTIONS应该是:

'driver': 'SQL Native Client',

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

于 2013-09-22T19:36:31.350 回答