Last active
February 9, 2017 19:34
-
-
Save nixon1333/6813e246eee2508b99574a8917df6855 to your computer and use it in GitHub Desktop.
Run raw SQL query in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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