On Dec 5, 8:00 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
> he needs to supply data to the DB as python unicode objects..the
> strings from the file should be decoded first from ISO-8859.
Thanks for the help. After some experimentation I found that encoding
all string data to UTF-8, ignoring errors, did the trick:
s = unicode(s, "utf-8", "ignore")
In case anybody is curious, here are tests to demonstrate my problem
and some solutions. N.B. UTF-8/replace worked in these tests, but not
in my SQLElixir application.
----
import unittest, sqlite3
class TestCase(unittest.TestCase):
def setUp(self):
self.conn = sqlite3.connect(":memory:")
self.conn.cursor().execute('CREATE TABLE demo (value TEXT)')
def insertAndDump(self, encoding, errors, expectFailure=False):
caseName = repr([encoding, errors])
data = 'K\xf6 1366'
try:
if encoding is not None:
data = unicode(data, encoding=encoding, errors=errors)
cursor = self.conn.cursor()
cursor.execute("INSERT INTO demo (value) VALUES (?)",
[data])
cursor.execute('SELECT * FROM demo')
self.failUnless(len(cursor.fetchall()) == 1)
self.failIf(expectFailure, "Unexpected success for %s" %
caseName)
except Exception, info:
self.failUnless(expectFailure, "Failed %s: %s" %
(caseName, info))
def testCannotRetrieveUnencoded(self):
self.insertAndDump(None, None, True)
def testCannotEncodeStrict(self):
self.insertAndDump("utf-8", "strict", True)
def testCanRetrieve8859Strict(self):
self.insertAndDump("8859", "strict")
def testCanRetrieveUTF8Ignored(self):
self.insertAndDump("utf-8", "ignore")
def testCanRetrieveUTF8Replaced(self):
self.insertAndDump("utf-8", "replace")
unittest.main()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---