Setting the encoding in pysqlite2

Michele Simionato michele.simionato at gmail.com
Thu Aug 25 10:15:55 CEST 2005


An easy question, but I don't find the answer in the docs :-(
I have a sqlite3 database containing accented characters (latin-1).
How do I set the right encoding? For instance if I do this:

#-*- encoding: latin-1 -*-
from pysqlite2 import dbapi2 as sqlite
import os

DBFILE="/tmp/example.db"

def writedb(conn):
    c = conn.cursor()
    c.executescript("""
    create table example (word char(20));
    insert into example values ("così");
    """)
    c.close()

def readdb(conn):
    c = conn.cursor()
    c.execute("select * from example;")
    #print c.fetchall()
    c.close()

if __name__ == "__main__":
    conn = sqlite.connect(DBFILE)
    writedb(conn)
    readdb(conn)
    conn.close()
    os.remove(DBFILE)

I get UnicodeDecodeError: 'utf8' codec can't decode byte 0xec in
position 3: unexpected end of data
(notice, even if the 'print' statement is commented.


        Michele Simionato




More information about the Python-list mailing list