原文:https://blog.youkuaiyun.com/zhuangmezhuang/article/details/82776272
Python 3.7 还有其他解决方式:
https://blog.youkuaiyun.com/zd147896325/article/details/80092563
一、问题
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tech/poc/env/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
self.check_migrations()
File "/tech/poc/env/lib/python3.5/site-packages/django/core/management/base.py", line 422, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/loader.py", line 209, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/tech/poc/env/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/tech/poc/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 198, in get_new_connection
conn = Database.connect(**conn_params)
django.db.utils.NotSupportedError: URIs not supported
二:回答
I have the same problem, it’s broke on my machine, but all my co-workers don’t get the bug. I debugged in django’s code, and found that in django.db.backends.sqlite3.base on about line 194 it sets uri True. If i comment that out, it works.
这个answer没有直面说解决方法,说了问题的出现在哪里,就是在这个base.py的大概194行左右,有个True。这里出了问题。
但是并没有告诉你怎么修改这里的代码。
原因分析
这是sqlite3版本3.7的通病,安装3.8的就可以,这里我采用修改源码的方式,直接打开。
"/home/python3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py"这个文件,修改修改大概198行,通过搜索uri查找。
# between multiple threads. The safe-guarding will be handled at a
# higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
# property. This is necessary as the shareability is disabled by
# default in pysqlite and it cannot be changed once a connection is
# opened.
if 'check_same_thread' in kwargs and kwargs['check_same_thread']:
warnings.warn(
'The `check_same_thread` option was provided and set to '
'True. It will be overridden with False. Use the '
'`DatabaseWrapper.allow_thread_sharing` property instead '
'for controlling thread shareability.',
RuntimeWarning
)
kwargs.update({'check_same_thread': False})
if self.features.can_share_in_memory_db:
kwargs.update({'uri': False}) #这里原来是True,修改为False就可以了
return kwargs
把上面的uri对应True修改为False就ok了。
附加
2.tar -xvzf sqlite-autoconf-3081101.tar.gz
3.编译安装:
(1)./configure
(2)make
(3)sudo make install
(4)sudo ldconfig
博客围绕Python Django中sqlite3的问题展开。指出问题出现在django.db.backends.sqlite3.base文件约194行处将uri设为True。分析原因是sqlite3版本3.7的通病,给出两种解决办法,一是修改源码将uri对应True改为False,二是编译安装sqlite3 3.8版本。

被折叠的 条评论
为什么被折叠?



