我如何检查数据库连接到django中的mysql [英] how can I check database connection to mysql in django
本文介绍了我如何检查数据库连接到django中的mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想,我可以从数据库中读取一些东西,但是看起来太多了,有什么样的吗?:
settings.DATABASES ['default']。check_connection()
解决方案
所有您需要做的是启动应用程序,如果未连接,它将失败。其他方式你可以尝试在shell尝试以下 -
从django.db导入连接
从django.db。 utils import OperationalError
db_conn = connections ['default']
try:
c = db_conn.cursor()
除了OperationalError:
connected = False
else :
connected = True
how can I do it?
I thought, I can read something from database, but it looks too much, is there something like?:
settings.DATABASES['default'].check_connection()
解决方案
All you need to do is start a application and if its not connected it will fail. Other way you can try is on shell try following -
from django.db import connections
from django.db.utils import OperationalError
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
这篇关于我如何检查数据库连接到django中的mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文