This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2009年05月13日 10:35 by izarf, last changed 2022年04月11日 14:56 by admin. This issue is now closed.
| Messages (4) | |||
|---|---|---|---|
| msg87672 - (view) | Author: (izarf) | Date: 2009年05月13日 10:35 | |
it is impossible to retrieve a latin-1 encoded string from sqlite3 database. How to: 1. create a new db. 2. create a new table with text field. 3. insert a row with data like "åäö" 4. select all rows from table 5. write: for data in cursor1: var = data you will now get an error explaining something like ascii couldn't decode |
|||
| msg87682 - (view) | Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) | Date: 2009年05月13日 12:21 | |
Confirmed here, tested with python2.6
on Linux with sys.stding.encoding == 'ISO-8859-1'
on Windows with sys.stdin.encoding == 'cp437'
>>> import sqlite3
>>> db = sqlite3.connect(':memory:')
>>> cur = db.cursor()
>>> cur.execute("create table foo (x)")
>>> cur.execute("insert into foo values ('café')")
>>> cur.execute("select * from foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: Could not decode to UTF-8 column 'x' with text
'café'
It seems that sqlite3 expects strings to be utf-8 encoded.
It works fine if you pass unicode strings, and with python 3.0.
|
|||
| msg87699 - (view) | Author: Martin v. Löwis (loewis) * (Python committer) | Date: 2009年05月13日 18:37 | |
This is not a bug. By default, pysqlite decodes all strings to Unicode,
assuming UTF-8 encoding (which SQLite assumes when parsing statements).
To override this default, you need to change the connection's text_factory:
py> import sqlite3
py> db = sqlite3.connect(':memory:')
py> db.text_factory = str
py> cur = db.cursor()
py> cur.execute("create table foo (x)")
<sqlite3.Cursor object at 0xb7cfb500>
py> cur.execute("insert into foo values ('café')")
<sqlite3.Cursor object at 0xb7cfb500>
py> cur.execute("select * from foo")
<sqlite3.Cursor object at 0xb7cfb500>
py> _.fetchall()
[('caf\xe9',)]
|
|||
| msg178584 - (view) | Author: Ezio Melotti (ezio.melotti) * (Python committer) | Date: 2012年12月30日 16:23 | |
While trying to reproduce the issue I noticed this while inserting values:
import sqlite3
db = sqlite3.connect(':memory:')
cur = db.cursor()
cur.execute("create table foo (x)")
# this works fine
cur.execute(u"insert into foo values ('café')".encode('latin1'))
# this fails
cur.execute(u"insert into foo values (?)", (u'café'.encode('latin1'),))
# this fails too
cur.execute("insert into foo values (?)", (u'café'.encode('latin1'),))
The error is:
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
Should this be reported in the first case too? (This would be backward-incompatible, but, unless it's expected to work, we can always add a warning.)
|
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:56:48 | admin | set | github: 50260 |
| 2012年12月31日 15:06:28 | jcea | set | nosy:
+ jcea |
| 2012年12月30日 16:32:42 | ezio.melotti | link | issue16783 superseder |
| 2012年12月30日 16:23:25 | ezio.melotti | set | versions:
+ Python 2.7, - Python 2.5 nosy: + ezio.melotti messages: + msg178584 components: + Library (Lib), - None |
| 2009年05月13日 18:37:32 | loewis | set | status: open -> closed nosy: + loewis messages: + msg87699 resolution: not a bug |
| 2009年05月13日 12:21:23 | amaury.forgeotdarc | set | nosy:
+ amaury.forgeotdarc messages: + msg87682 |
| 2009年05月13日 10:35:06 | izarf | create | |