1、之前一直正常运行,忽然就不能用了,报下图错误
解决:在浏览器上删除cookie后,刷新网页即可
2、使用SQLserver时报下边的错:django.core.exceptions.ImproperlyConfigured: ‘sql_server.pyodbc’ isn’t an available database backend.
解决:更新了下django的版本 ,之前安装其他包时被修改成了更低的版本
pip install --upgrade django==3.2.8 更新django
3、标记颜色
<font color=red size=22>color=#00ffff</font> 设置颜色
4、使用pyodbc时,指定了driver满足下图规则的driver,且有port,则port会以逗号的形式添加到SERVER关键词中,
测试时发现会导致连接失败,提示:pyodbc.OperationalError: (‘08001’, ‘[08001] [Microsoft][SQL Server Native Client 10.0]TCP Provider: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。\r\n (10060) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 10.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10060)’)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': '用户名',
'PASSWORD': '密码',
'NAME': '实际数据库名称'
},
'想要起的数据库名字': {
'ENGINE': 'sql_server.pyodbc', # 不一样
'HOST': '实际IP',
# 'PORT': 3433, 鉴于4所述
'USER': '用户名',
'PASSWORD': '密码',
'NAME': '实际数据库名称',
'OPTIONS': {
'driver': 'SQL Server Native Client 10.0', # 不一样
'MARS_Connection': True,
},
},
}
cursor = connections['default'].cursor() # 获取default的数据库的游标
cursor.execute(sql_search) # 执行SQL
5、调用流程:cursor = connections['default'].cursor()
C:\Users\admin\Envs\proj\Lib\site-packages\django\db\backends\base\base.py
@async_unsafe
def connect(self):
"""Connect to the database. Assume that the connection is closed."""
# Check for invalid configurations.
self.check_settings()
"""省略"""
# Establish the connection
conn_params = self.get_connection_params()
self.connection = self.get_new_connection(conn_params) # 3333
self.set_autocommit(self.settings_dict['AUTOCOMMIT'])
self.init_connection_state()
connection_created.send(sender=self.__class__, connection=self)
self.run_on_commit = []
@async_unsafe
def ensure_connection(self): # 2222
"""Guarantee that a connection to the database is established."""
if self.connection is None:
with self.wrap_database_errors:
self.connect()
def _cursor(self, name=None): # 1111
self.ensure_connection()
with self.wrap_database_errors:
return self._prepare_cursor(self.create_cursor(name))
C:\Users\admin\Envs\proj\Lib\site-packages\sql_server\pyodbc\base.py
def get_new_connection(self, conn_params):
"""省略"""
if ms_drivers.match(driver) and os.name == 'nt':
cstr_parts['MARS_Connection'] = 'yes'
# 将settings.py中的配置解析出来后,拼接成字符串
# 如:DRIVER={SQL Server Native Client 10.0};SERVER=127.0.0.1;DATABASE=DB_name;UID=User_Name;PWD=PassWord
connstr = encode_connection_string(cstr_parts)
"""省略"""
conn = None
"""省略"""
while conn is None:
try:
conn = Database.connect(connstr, # 4444 调用连接
unicode_results=unicode_results,
timeout=timeout)
except Exception as e:
"""省略"""
conn.timeout = query_timeout
return conn
import pyodbc
connection = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=127.0.0.1;DATABASE=DB_name;UID=User_Name;PWD=PassWord')
curs = connection.execute('select GETDATE()')
curs.fetchone()