如何将“使用”数据库传递给Django连接对象 [英] How to pass 'using' DB to the django connection object
本文介绍了如何将“使用”数据库传递给Django连接对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要查询django中的特定数据库,我可以执行以下操作:
To query a specific database in django I can do:
Item.objects.using('specific_db').all()
是否可以使用Django连接执行相同的操作?例如:
Is there a way to do the same using a django connection? For example:
>>> from django.db import connection
>>> cursor=connection.using('specific_db').cursor()
如果没有,我怎么能得到
If not, how could I get a cursor/connection for a specific DB without manually providing all the credentials?
推荐答案
根据在多个数据库上使用原始SQL ,您将使用连接
而不是连接
:
According the the django documentation on Using raw SQL on multiple databases, you would use connections
rather than connection
:
from django.db import connections
cursor = connections['specific_db'].cursor()
cursor.execute("select * from item")
这篇关于如何将“使用”数据库传递给Django连接对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文