[Tutor] Converting from unicode to nonstring

Peter Otten __peter__ at web.de
Fri Oct 15 14:36:07 CEST 2010

David Hutto wrote:

> Hey Buddy Pals,


> I receive the following output from a sqlite db
> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10')

How did the string u"1,2,3,4" get into the database in the first place?
The sqlite3 module offers a mechanism to convert data from and to Python 

import sqlite3
import json

sqlite3.register_adapter(list, json.dumps)
sqlite3.register_converter("list", json.loads)

db = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = db.cursor()

cursor.execute("create table data (value list)")
cursor.execute("insert into data values (?)", ([11,22,33],))
for row in cursor.execute("select value from data"):
    print row

See also:


More information about the Tutor mailing list