django connection cursor code example
Example 1: raw query in django
from django.db import connection
sql = 'SELECT * FROM db1.a AS a
JOIN db2.b AS b
ON b.some_id = a.id
WHERE a.name = %s'
cursor = connection.cursor()
try:
cursor.execute(sql, ['localhost'])
row = cursor.fetchall()
except Exception as e:
cursor.close
Example 2: django connection cursor
from django.db import connection
with connection.cursor() as cursor:
cursor.execute('TRUNCATE TABLE table_name CASCADE')
Example 3: django swlite queries
>>> Entry.objects.filter(pub_date__lte='2006-01-01')