diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-01-18 16:15:15 +0000 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2019-01-18 16:15:15 +0000 |
| commit | 49777de74cf3b9c7f369634020d39ddadc72bfaf (patch) | |
| tree | 06b1da786d9203b64042cae60f5762af6aa675e0 /doc/src/usage.rst | |
| parent | ddbe495d707afc141767da00fadf86cf9429b7c0 (diff) | |
| download | psycopg2-49777de74cf3b9c7f369634020d39ddadc72bfaf.tar.gz | |
Added documentation for BYTES casterregister-bytes
Diffstat (limited to 'doc/src/usage.rst')
| -rw-r--r-- | doc/src/usage.rst | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/doc/src/usage.rst b/doc/src/usage.rst index e9416e3..08c6dce 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -457,13 +457,29 @@ the connection or globally: see the function Unicode, you can register the related typecasters globally as soon as Psycopg is imported:: - import psycopg2 import psycopg2.extensions psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) and forget about this story. +.. note:: + + In some cases, on Python 3, you may want to receive `!bytes` instead of + `!str`, without undergoing to any decoding. This is especially the case if + the data in the database is in mixed encoding. The + `~psycopg2.extensions.BYTES` caster is what you neeed:: + + import psycopg2.extensions + psycopg2.extensions.register_type(psycopg2.extensions.BYTES, conn) + psycopg2.extensions.register_type(psycopg2.extensions.BYTESARRAY, conn) + cur = conn.cursor() + cur.execute("select %s::text", (u"€",)) + cur.fetchone()[0] + b'\xe2\x82\xac' + + .. versionadded: 2.8 + .. index:: single: Buffer; Adaptation |
