Dictionary Raw SQL Queries Django

Subscribe dengan Account Google untuk mendapatkan News Letter terbaru dari Halovina !
Dictionary Raw SQL Queries Django
Lenovo IdeaPad Slim 3 14ABR8 AMD Ryzen 5

Lenovo IdeaPad Slim 3 14ABR8 AMD Ryzen 5

Lenovo IdeaPad Slim 3 14ABR8 82XL0004ID - ABYSS BLUE = LENOVO SLIM 3 82XL007CID R5-7530U/8GB/512GB/14.0/WIN11+OHS/ABYSS BLUE, Lenovo IdeaPad Slim 3 14ABR8 82XL0005ID - ARCTIC GREY = LENOVO SLIM 3 82XL007BID R5-7530U/8GB/512GB/14.0/WIN11+OHS/ARCTIC GREY

Free Klik Disini !

Bagaimana cara membuat dictionary pada raw SQL queri django framework ?


1) import library django connection

from django.db import connections

 

2) buat fungsi untuk merubah index menjadi dictionary

def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]

 

3) buat fungsi untuk memanggil data dari database

 

def get_list_user():
with connections['example_db'].cursor() as cursor:
cursor.execute("""select * from users """)
connection.close()
row = dictfetchall(cursor)
return row

 

4) Extract data

for x in get_list_user():
print(x['username'])

 

good luck ...!

Baca artikel lainya: