pythonsqlite

Python's sqlite3.register_converter() does not convert double to Decimal


In sqlite3 there is this table:

create table t(d double)  

In python, the code is:

sqlite3.register_converter('double', Decimal)
...
for d, in connection.execute('select d from t limit 1'):
    print type(d)

The printed result is: <type 'float'>
Any idea why?


Solution

  • connect to the database using the optional argument:

    detect_types=sqlite3.PARSE_DECLTYPES
    

    ie:

    conn = sqlite3.connect('database.db', detect_types=sqlite3.PARSE_DECLTYPES)
    

    then continue as you have. It should work.

    see:

    http://www.gossamer-threads.com/lists/python/python/627047