1. Home
  2. Computer Science MCQs
  3. Django
  4. In Django, how can you perform raw SQL queries?

Added 28 Apr 2024

In Django, how can you perform raw SQL queries?

  1. Using the django.db.connection object Correct answer
  2. Using the django.db.models module
  3. By subclassing the django.db.models.QuerySet class
  4. By modifying the settings.py file

Correct answer

A. Using the django.db.connection object

15 views

MCQSEXAM

Explanation

Raw SQL can be run using the django.db.connection object: open a cursor with connection.cursor(), call execute() and fetch the rows.

The alternative is Model.objects.raw(), which returns model instances. Always pass parameters separately to avoid SQL injection.

For more solved questions like this one, see Computer Science MCQs on MCQsExam.com.

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Django MCQs

Practise all Django MCQs

Similar MCQs