Re: try to use bool/booleans with adapter/converter, but...

Martin Jenkins <sqliteREMOVE-Vfh7fEhEWOlaa/[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Organization XQP Ltd
Message-ID <[email protected]>
henk-jan ebbers wrote:
> Greetings,
> 
> I am trying to store booleans in SQLite with pysqlite.
> 
> But somehow this does not work.
> I get 'Operation Error: no such column: True'
> It seems that my adapter/convert-functions are never executed

What version of Python are you using?

PythonWin 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' 
for further copyright information.
 >>>
 >>> def converter4bool(intfromdb):
... 	ret=bool(intfromdb)
... 	print "Converted",intfromdb, "to", ret
... 	return ret
...
 >>>
 >>> def adapter4bool(boolfrompython):
... 	ret=int(boolfrompython)
... 	print "Adapted", boolfrompython, "to", ret
... 	return ret
...
 >>>
 >>> import sqlite3
 >>>
 >>> sqlite3.register_converter('BOOLEAN',converter4bool)
 >>> sqlite3.register_adapter(bool,adapter4bool)
 >>>
 >>> C=sqlite3.Connection(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
 >>>
 >>> C.execute("""CREATE TABLE partner (idpartner VARCHAR(35) NOT NULL, 
active BOOLEAN, name VARCHAR(256), PRIMARY KEY (idpartner))""")
<sqlite3.Cursor object at 0x00D8A260>
 >>>
 >>> C.execute("insert into partner values(?,?,?)", ("Martin-ID", True, 
"Martin-name"))
Adapted True to 1
<sqlite3.Cursor object at 0x00D8A1D0>
 >>>
 >>> C.execute("select * from partner").fetchall()
Converted 1 to True
[(u'Martin-ID', True, u'Martin-name')]
 >>>

Martin
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.