如何在Sql Server中使用Django [英] How to use Django with Sql Server
问题描述
我通常在Django中使用Postgres来满足我的数据库需求,但是最近我在一家在Windows环境中使用MSSQL的公司开始工作.长话短说,我不得不重写settings.py中的数据库属性.不幸的是,我不知道如何使用Pyodbc连接到SQL Server,并且它们正在运行Python 3.x,所以我不能使用Django-Pyodbc.在尝试运行它时,我得到了:找不到数据源名称,并且未指定默认驱动程序(0)(SQLDriverConnect)')"
I normally use Postgres for my database needs with Django but I recently started at a company which use MSSQL on a Windows environment. Long story short I had to rewrite the database properties in settings.py. Unfortunately, I have NO idea how to connect to a SQL Server using Pyodbc and they're running Python 3.x so I can't use Django-Pyodbc. While trying to run it I'm getting a: "Data source name not found and no default driver specified (0) (SQLDriverConnect)')"
这是我当前的数据库配置.我可能做错了事,但是很难找到资源,因为大多数Django + Sql Server结果要么使用FreeTDS要么使用Django-Pyodbc(都不是选项).
Here is my current db config as it stands. I'm probably doing something wrong but it is very difficult to find resources since most Django+Sql Server results either use FreeTDS or Django-Pyodbc (neither are options).
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name_on_server',
'USER': 'my_acct',
'PASSWORD': 'nope',
'HOST': 'x.x.x.x',
'PORT': '1433',
'OPTIONS': { # Options are not edited
'driver': 'SQL Server', # What it displays as on odbc admin
'dsn': 'System DSN', # What it displays as on odbc admin
'use_legacy_datetime': False
}
推荐答案
旧问题,但这可能会对某人有所帮助.这些是我在Windows中用于连接到SQL Server的设置.正如@flipperpa所说,我还使用 django-pyodbc-azure
Old question, but it might help someone. These are the settings I use in Windows to connect to a SQL Server. As @flipperpa said, I also use django-pyodbc-azure
pyodbc==3.0.10
django-pyodbc-azure==1.10.0.1
但是,pip下载的pyodbc对我不起作用.按照在此答案中建议,请访问此网站
However, the pyodbc that pip will download doesn't work for me. As suggested in this answer, go this site http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc and download either depending on the version of Python 3.5 you have installed:
-
pyodbc‑3.0.10‑cp35‑none‑win32.whl
(如果您安装了32位Python 3.5) -
pyodbc‑3.0.10‑cp35‑none‑win_amd64.whl
(如果您安装了64位Python 3.5)
pyodbc‑3.0.10‑cp35‑none‑win32.whl
if you have a 32-bit Python 3.5 installpyodbc‑3.0.10‑cp35‑none‑win_amd64.whl
if you have a 64-bit Python 3.5 install
然后使用以下设置进行连接:
Then use these settings to connect:
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': "localhost",
'USER': "--user name--",
'PASSWORD': "--password--",
'NAME': "--database name--",
'PORT': 1433,
'OPTIONS': {
'driver' : 'SQL Server Native Client 11.0',
'MARS_Connection' : True,
'driver_supports_utf8' : True,
},
}
对于任何使用linux的人,MS现在已经发布了用于SQL Server的官方odbc驱动程序,并且正式(如果不是默认的话)支持django-pyodbc-azure项目.我高度建议使用它以及FreeTDS上的本机linux SQL Server ODBC驱动程序.
For anyone that is using linux, MS has now released official odbc drivers for SQL server and is officially (if more than a little tacitly) supporting the django-pyodbc-azure project. I highly recommend using it and also the native linux SQL Server ODBC driver over FreeTDS.
这篇关于如何在Sql Server中使用Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!