On Oct 1, 2010, at 3:48 PM, Stratos Papadopoulos wrote:
> Hello All,
>
> I have to store encrypted unicode content in specific database columns
> (until now I used sqlalchemy).
> Elixir supports column encryption, but I am not able to get back from
> the database unicode strings
> (to use them directly in a web app)
>
> the following code stores unicode in the db and retrieves unicode
> strings from the db, directly.
>
> ------------------------
> from elixir import *
>
> class Person(Entity):
> name = Field(Unicode)
> password = Field(Unicode)
> ssn = Field(Unicode)
> ---------------------
> # -*- coding: UTF-8 -*-
> from elixir import *
> from model import *
>
> metadata.bind="postgresql://webuser:1...@localhost:5432/test_db"
> metadata.bind.echo=True
>
> setup_all()
> create_all()
>
> Person(name=u'Στράτος', password='Στράτος', ssn='Στράτος')
>
> Session.commit()
>
> person = Session.query(Person).first()
> print type(person.password)
> ---------------------
> program run output:
> Στράτος <type 'unicode'>
>
>
> The respective code with encryption (it is the example in the source
> code):
>
> ------------------------
>
> from elixir import *
> from elixir.ext.encrypted import acts_as_encrypted
>
> class Person(Entity):
> name = Field(Unicode)
> password = Field(Unicode)
> ssn = Field(Unicode)
>
> acts_as_encrypted(for_fields=['password', 'ssn'],
> with_secret='secret')
>
> ------------------------
> program run output:
> Στράτος <type 'str'>
>
>
> I get also a warning fron SQLA:
> SAWarning: Unicode type received non-unicode bind param value.
> param[key.encode(encoding)] = processors[key](compiled_params[key])
>
> Is this the 'normal' behavior?
> Is there a way to get unicode strings from the db directly?
> I am using elixir 0.7.1 and sqlalchemy 0.6.4.
>
> The only way to bypass the warning is to use String instead of Unicode
> for the encrypted columns and encode to string the unicode inputs.
>
> ------------------------
> class Person(Entity):
> name = Field(Unicode)
> password = Field(String)
> ssn = Field(String)
>
> acts_as_encrypted(for_fields=['password', 'ssn'],
> with_secret='secret')
> ------------------------
> ....
> Person(name=u'Στράτος', password='Στράτος'.encode('utf-8'),
> ssn='Στράτος'.encode('utf-8'))
> ....
> ------------------------
Your problem is that you don't use unicode objects, but instead pass strings.
That's the reason for the warning.
Which is ok if you are working only within the ascii-range (as that can be
converted easily), but apparently you don't.
SLo, use
u"<funny characters>"
which is a Python unicode literal.
I suggest you read the python unicode howto or something.
Diez
--
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en.