Django 应该连接到 MSSQL
Django should connect to MSSQL
我正在使用 PyCharm、Django 和 MSSQL-Backend-Project。
我正在使用我的 iMac,在本地网络中有一个我想连接的 Microsoft SQL 服务器。
我不确定要使用什么凭据(IP,或者下面指定的主机)。但是,第一次尝试时,我遇到了很多错误。对我来说哪个接缝不是凭据问题,而是安装错误或其他问题。
我已经安装了一个 ne 虚拟环境(我在 google 上找到的建议)。但是还是不行。
任何人都可以帮助我,从哪里开始?
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'bnt': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'POS',
'USER': 'l.bamberg',
'PASSWORD': '****************',
'HOST': 'BNTS20005\SQL17STD',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
错误信息:
Traceback (most recent call last):
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
signals.request_started.send(sender=self.__class__, environ=environ)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
for conn in connections.all():
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
signals.request_started.send(sender=self.__class__, environ=environ)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
for conn in connections.all():
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET /favicon.ico HTTP/1.1" 500 59```
按照@EgorWexler 所说继续并安装 libodbc
驱动程序如下:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
到目前为止一切正常。
我正在使用 PyCharm、Django 和 MSSQL-Backend-Project。
我正在使用我的 iMac,在本地网络中有一个我想连接的 Microsoft SQL 服务器。
我不确定要使用什么凭据(IP,或者下面指定的主机)。但是,第一次尝试时,我遇到了很多错误。对我来说哪个接缝不是凭据问题,而是安装错误或其他问题。
我已经安装了一个 ne 虚拟环境(我在 google 上找到的建议)。但是还是不行。
任何人都可以帮助我,从哪里开始?
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'bnt': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'POS',
'USER': 'l.bamberg',
'PASSWORD': '****************',
'HOST': 'BNTS20005\SQL17STD',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
}
}
错误信息:
Traceback (most recent call last):
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
signals.request_started.send(sender=self.__class__, environ=environ)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
for conn in connections.all():
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 11, in <module>
import pyodbc as Database
ImportError: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/contrib/staticfiles/handlers.py", line 76, in __call__
return self.application(environ, start_response)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 131, in __call__
signals.request_started.send(sender=self.__class__, environ=environ)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in send
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp>
for receiver in self._live_receivers(sender)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/__init__.py", line 27, in reset_queries
for conn in connections.all():
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in all
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 76, in <listcomp>
return [self[alias] for alias in self]
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 13, in <module>
raise ImproperlyConfigured("Error loading pyodbc module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: dlopen(/Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): Library not loaded: /usr/local/opt/unixodbc/lib/libodbc.2.dylib
Referenced from: /Users/georghuber/Desktop/Waschhalle/venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so
Reason: tried: '/usr/local/opt/unixodbc/lib/libodbc.2.dylib' (no such file), '/usr/local/lib/libodbc.2.dylib' (no such file), '/usr/lib/libodbc.2.dylib' (no such file)
[05/Feb/2022 13:15:52] "GET /favicon.ico HTTP/1.1" 500 59```
按照@EgorWexler 所说继续并安装 libodbc
驱动程序如下:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
到目前为止一切正常。