2013-08-14 26 views
5

Tôi có một dự án Django sử dụng nhiều cơ sở dữ liệu. https://docs.djangoproject.com/en/dev/topics/db/multi-db/Django, nhiều cơ sở dữ liệu với sql thô. Làm thế nào để chọn db?

tôi thực hiện rất nhiều thắc mắc liệu như thế này:

cursor = connection.cursor() 
    cursor.execute("select * from my_table") 
    .... 
    transaction.commit_unless_managed() 

Làm thế nào tôi có thể xác định cơ sở dữ liệu sử dụng không?

+0

Bạn đã thử 'transaction.commit_unless_managed (using = 'database_entry')'? – arulmr

+0

Không ai có liên quan đến tài liệu thực tế, rất rõ ràng: https://docs.djangoproject.com/en/dev/topics/db/transactions/ –

Trả lời

14

Tham khảo tài liệu django trên executing custom query directly. Xác định cơ sở dữ liệu trong kết nối của bạn như được đưa ra dưới đây:

from django.db import connections 
cursor = connections['db_alias'].cursor() 
# Your code here... 

Và sau đó cam kết sử dụng

transaction.commit_unless_managed(using='db_alias') 
0

thử này có thể là nó nên tác phẩm.

from django.db import connections 
cursor = connections[’my_db_name’].cursor() 
# Your code here... 
transaction.commit_unless_managed(using=’my_db_name’) 
Các vấn đề liên quan