from django.db import connection
cursor=connection.cursor()
# 如果需要配置数据库
# cursor=connection['default'].cursor()
cursor.execute('select * from app01_book')
ret=cursor.fetchall()
print(ret)
#((2, '小时光', Decimal('10.00'), 2), (3, '未来可期', Decimal('33.00'), 1), (4, '打破思维里的墙', Decimal('11.00'), 2), (5, '时光不散', Decimal('11.00'), 3))
raw需要很复杂的查询(不好用)
# row方法:(掺杂着原生sql和orm来执行的操作)
res = CookBook.objects.raw('select id as nid from epos_cookbook where id>%s', params=[1, ])
print(res.columns) # ['nid']
print(type(res)) # <class 'django.db.models.query.RawQuerySet'>
# 在select里面查询到的数据orm里面的要一一对应
res = CookBook.objects.raw("select * from epos_cookbook")
print(res)
for i in res:
print(i.create_date)
print(i)
res = CookBook.objects.raw('select * from epos_cookbook where id>%s', params=[1, ])
# 后面可以加参数进来
print(res)
for i in res:
# print(i.create_date)
print(i)