Re: Converters for built-in types

Gerhard Häring <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello Antonio,

Antonio Valente wrote:
> 
> No, I need to have cursor's result wrapped in an object of mine and not
> in python's strings when datatype is "char" or "varchar".
> But if I install a converter function, it doesn't work for varchar since
> I declare the column as "some_column varchar(20)": it looks for a
> converter for "varchar(20)" type.
> [...]

There's currently no way to achieve this, apart from really just
registering the "varchar(20)" type.

Alternatively, you could play tricks with the row_factory feature of
pysqlite and convert unicode objects to objects of your own class, like this:

from pysqlite2 import dbapi2 as sqlite

class MyClass(str): pass

def my_row_factory(cursor, t):
    l = []
    for col in t:
        if type(col) is unicode:
            l.append(MyClass(col))
        else:
            l.append(col)
    return tuple(l)

con = sqlite.connect(":memory:")
con.row_factory = my_row_factory

cur = con.cursor()
cur.execute("select 'hello'")
print type(cur.fetchone()[0])

The various factories, including row factories are described in the
pysqlite docs.

- -- Gerhard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFGE0wdIO4ozGCH14RAqlcAKCHojd2v3+O1Ds0Hhfmus4BEkz7UgCeINWG
OzqW+JXKPI8VqjkSzO1Svp0=
=eI43
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.