summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-07-19 02:13:20 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-07-19 02:13:20 +0000
commit8f4fa5a4e7435876a99383bffafb7a09ba6667e1 (patch)
tree14536fe5ef2f7b25961998b87d3effbff8af4fea
parent00f54d6cf69fbe9a3970c038c2a67ee8c8307bca (diff)
Fixed #69 -- Implemented dictfetchone(), dictfetchmany() and dictfetchall() for mysql DB backend. Thanks, Manuzhai!
git-svn-id: http://code.djangoproject.com/svn/django/trunk@190 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/core/db/backends/mysql.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py
index cfea2f640e..817be0c19a 100644
--- a/django/core/db/backends/mysql.py
+++ b/django/core/db/backends/mysql.py
@@ -46,17 +46,26 @@ class DatabaseWrapper:
self.connection.close()
self.connection = None
+def _dict_helper(desc, row):
+ "Returns a dictionary for the given cursor.description and result row."
+ return dict([(desc[col[0]][0], col[1]) for col in enumerate(row)])
+
def dictfetchone(cursor):
"Returns a row from the cursor as a dict"
- raise NotImplementedError
+ row = cursor.fetchone()
+ if not row:
+ return None
+ return _dict_helper(cursor.description, row)
def dictfetchmany(cursor, number):
"Returns a certain number of rows from a cursor as a dict"
- raise NotImplementedError
+ desc = cursor.description
+ return [_dict_helper(desc, row) for row in cursor.fetchmany(number)]
def dictfetchall(cursor):
"Returns all rows from a cursor as a dict"
- raise NotImplementedError
+ desc = cursor.description
+ return [_dict_helper(desc, row) for row in cursor.fetchall()]
def get_last_insert_id(cursor, table_name, pk_name):
cursor.execute("SELECT LAST_INSERT_ID()")