﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
227	sqlite and unicode problems	hugo <gb@…>	Adrian Holovaty	"sqlite3 and pysqlite2 allways returns unicode stuff for values. This poses problems in django. To fix, just use a cursor factory and a row factory to patch out all unicode elements to utf8 bytestrings. This is the patch:

{{{
Index: core/db/backends/sqlite3.py
===================================================================
--- core/db/backends/sqlite3.py (revision 341)
+++ core/db/backends/sqlite3.py (working copy)
@@ -14,6 +14,13 @@
 Database.register_converter(""date"", typecasts.typecast_date)
 Database.register_converter(""datetime"", typecasts.typecast_timestamp)
 
+# a row factory that removes unicode #########################################
+def utf8row(cursor, row):
+        def utf8(s):
+                if type(s) == unicode: return s.encode('utf-8')
+                else: return s
+        return [utf8(el) for el in row]
+
 # Database wrapper ############################################################
 
 class DatabaseWrapper:
@@ -30,7 +37,7 @@
             self.connection.create_function(""django_date_trunc"", 2, _sqlite_date_trunc)
         if DEBUG:
             return base.CursorDebugWrapper(FormatStylePlaceholderCursor(self.connection), self)
-        return FormatStylePlaceholderCursor(self.connection)
+        return self.connection.cursor(factory=FormatStylePlaceholderCursor)
 
     def commit(self):
         self.connection.commit()
@@ -50,7 +57,11 @@
     This fixes it -- but note that if you want to use a literal ""%s"" in a query,
     you'll need to use ""%%s"" (which I belive is true of other wrappers as well).
     """"""
-    
+
+    def __init__(self, *args, **kw):
+        Database.Cursor.__init__(self, *args, **kw)
+        self.row_factory = utf8row
+
     def execute(self, query, params=[]):
         query = self.convert_query(query, len(params))
         return Database.Cursor.execute(self, query, params)
}}}
"	defect	closed	Database layer (models, ORM)	1.0	major	fixed	unicode sqlite		Design decision needed	0	0	0	0	0	0
