Skip to content

Instantly share code, notes, and snippets.

@nixon1333
Last active February 9, 2017 19:34
Show Gist options
  • Save nixon1333/6813e246eee2508b99574a8917df6855 to your computer and use it in GitHub Desktop.
Save nixon1333/6813e246eee2508b99574a8917df6855 to your computer and use it in GitHub Desktop.
Run raw SQL query in Django
#it's the system's default database connection
from django.db import connection
def use_raw_query(self):
#raw sql query here
query = "SELECT * FROM `table` WHERE `row`= 1 "
#a cursor object
with connection.cursor() as cursor:
#execute the query
cursor.execute(query)
#get all the rows as a list
rows = cursor.fetchall()
return rows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment