另一个开发人员,我正在设置使用另一台服务器上遗留的SQL Server数据库(SQLEXPRESS)一个Django(。5.0)项目。 到目前为止,我们已经能够连接到来自Linux和Mac使用的Django pyodbc数据库,并使用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 /后端
- 创建一个PYTHONPATH环境变量指向/ Django的/ DB /后端/ SQL_SERVER
- 如果不能确保它应该指向/站点包/ SQL_SERVER呢?
- 创建ODBC数据源(系统DSN)
- 测试连接选项作品
- Editted settings.py中的数据库条目是几乎完全一样的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'