How do I close a database cursor django 1.8 and a connection?

Here is what I have:

from django.db import connection

class Command(BaseCommand):

option_list = BaseCommand.option_list

def handle(self, *labels, **options):
    with connection.cursor() as cursor:
        # Drop database
        cursor.execute("drop database if exists test_db;")
        # Create database again
        cursor.execute("create database test_db;")

Where in this block can I close the db cursor and connection and what do I call to close them?

My suggestion is try to create and close the cursor within each method where query is needed.

cursor = connection.cursor()
cursor.execute(query)
cursor.close()

So your function should look like this:

def handle(self, *labels, **options):
    with connection.cursor() as cursor:
        # Drop database
        cursor.execute("drop database if exists test_db;")
        # Create database again
        cursor.execute("create database test_db;")
        #Close the cursor
        cursor.close()
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » How do I close a database cursor django 1.8 and a connection?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录