另一位开发人员和我正在另一台服务器上使用旧版SQL服务器数据库(SQLEXPRESS)设置django(v1.4.2)项目。到目前为止,我们已经能够使用django-pyodbc从linux和mac连接数据库,并使用django-mssql从运行Windows 7的笔记本电脑连接到数据库。我想在笔记本电脑上使用django-pyodbc来保持环境同步。
在笔记本电脑上:
因此,它不起作用,我收到以下错误消息,并且不知道下一步该做什么:
('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 ---- 这是最终奏效的:
DATABASES设置中的部分输入:
'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'
答案 0 :(得分:2)
尝试使用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',
编辑:哎呀,没看到你解决了问题。在此留下我的答案作为参考。