1. from django.db import connection
    2. cursor=connection.cursor()
    3. # 如果需要配置数据库
    4. # cursor=connection['default'].cursor()
    5. cursor.execute('select * from app01_book')
    6. ret=cursor.fetchall()
    7. print(ret)
    8. #((2, '小时光', Decimal('10.00'), 2), (3, '未来可期', Decimal('33.00'), 1), (4, '打破思维里的墙', Decimal('11.00'), 2), (5, '时光不散', Decimal('11.00'), 3))

    raw需要很复杂的查询(不好用)

    1. # row方法:(掺杂着原生sqlorm来执行的操作)
    2. res = CookBook.objects.raw('select id as nid from epos_cookbook where id>%s', params=[1, ])
    3. print(res.columns) # ['nid']
    4. print(type(res)) # <class 'django.db.models.query.RawQuerySet'>
    5. # select里面查询到的数据orm里面的要一一对应
    6. res = CookBook.objects.raw("select * from epos_cookbook")
    7. print(res)
    8. for i in res:
    9. print(i.create_date)
    10. print(i)
    11. res = CookBook.objects.raw('select * from epos_cookbook where id>%s', params=[1, ])
    12. # 后面可以加参数进来
    13. print(res)
    14. for i in res:
    15. # print(i.create_date)
    16. print(i)