Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
5ab65ca5
Kaydet (Commit)
5ab65ca5
authored
Agu 29, 2015
tarafından
Dražen Odobašić
Kaydeden (comit)
Tim Graham
Eyl 03, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #25326 -- Added namedtuple example for executing custom SQL.
üst
40bf18e7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
8 deletions
+30
-8
sql.txt
docs/topics/db/sql.txt
+30
-8
No files found.
docs/topics/db/sql.txt
Dosyayı görüntüle @
5ab65ca5
...
@@ -275,28 +275,50 @@ alias::
...
@@ -275,28 +275,50 @@ alias::
cursor = connections['my_db_alias'].cursor()
cursor = connections['my_db_alias'].cursor()
# Your code here...
# Your code here...
By default, the Python DB API will return results without their field
By default, the Python DB API will return results without their field
names,
names, which means you end up with a ``list`` of values, rather than
a
which means you end up with a ``list`` of values, rather than a ``dict``. At
a
``dict``. At a small performance cost, you can return results as a
small performance and memory cost, you can return results as a ``dict`` by
``dict`` by
using something like this::
using something like this::
def dictfetchall(cursor):
def dictfetchall(cursor):
"Return
s
all rows from a cursor as a dict"
"Return all rows from a cursor as a dict"
desc = cursor.description
desc = cursor.description
return [
return [
dict(zip([col[0] for col in desc], row))
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
for row in cursor.fetchall()
]
]
Here is an example of the difference between the two::
Another option is to use :func:`collections.namedtuple` from the Python
standard library. A ``namedtuple`` is a tuple-like object that has fields
accessible by attribute lookup; it's also indexable and iterable. Results are
immutable and accessible by field names or indices, which might be useful::
from collections import namedtuple
def namedtuplefetchall(cursor):
"Return all rows from a cursor as a namedtuple"
desc = cursor.description
nt_result = namedtuple('Result', [col[0] for col in desc])
return [nt_result(*row) for row in cursor.fetchall()]
Here is an example of the difference between the three::
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> cursor.fetchall()
>>> cursor.fetchall()
((54360982
L, None), (54360880L
, None))
((54360982
, None), (54360880
, None))
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> dictfetchall(cursor)
>>> dictfetchall(cursor)
[{'parent_id': None, 'id': 54360982L}, {'parent_id': None, 'id': 54360880L}]
[{'parent_id': None, 'id': 54360982}, {'parent_id': None, 'id': 54360880}]
>>> cursor.execute("SELECT id, parent_id FROM test LIMIT 2");
>>> results = namedtuplefetchall(cursor)
>>> results
[Result(id=54360982, parent_id=None), Result(id=54360880, parent_id=None)]
>>> results[0].id
54360982
>>> results[0][0]
54360982
Connections and cursors
Connections and cursors
-----------------------
-----------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment