Re: sqlite3 adaptors mystery



On 2008-03-01, 23:41 GMT, Mel wrote:
There's nothing much wrong. cur.fetchall is returning a list
of all the selected rows, and each row is a tuple of fields.
Each tuple is being converted for display by repr, so the
strings are shown as unicode, which is what they are
internally. Change the print to

for (field,) in cur.fetchall():
print field

and you'll see your plain-text strings.

Thanks for your help, but plain-text strings is not what
I wanted. The boolean variables was what I was after. See this
modified version of the script:

#!/usr/bin/python
import sqlite3
def adapt_boolean(bol):
if bol:
return "True"
else:
return "False"

def convert_boolean(bolStr):
if str(bolStr) == "True":
return bool(True)
elif str(bolStr) == "False":
return bool(False)
else:
raise ValueError, "Unknown value of bool attribute
'%s'" % bolStr

sqlite3.register_adapter(bool,adapt_boolean)
sqlite3.register_converter("boolean",convert_boolean)

db = sqlite3.connect(":memory:")
cur=db.cursor()
cur.execute("create table test(p boolean)")
p=False
cur.execute("insert into test(p) values (?)", (p,))
p=True
cur.execute("insert into test(p) values (?)", (p,))
cur.execute("select p from test")
for (field,) in cur.fetchall():
print field,type(field)

The output here is:

[matej@viklef dumpBugzilla]$ python testAdaptors.py False <type
'unicode'>
True <type 'unicode'>
[matej@viklef dumpBugzilla]$

I thought that converter is there for just exactly this -- that
I would get back bool values not strings.

Sorry for not being clear in the first run.

Matej
.



Relevant Pages

  • Re: Syntax question and String Comparison
    ... they are overloaded to also act as non-short-circuiting logical operators ... Instant VB: C# to VB converter ... strings to an arraylist and do a binary search. ... The & operator is used for bitwise "and" operations. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: General programming question
    ... in C#. I've created my PET/ASCII converter which works fine for byte arrays, but I'm having trouble with strings because .Net doesn't support 8-bit ASCII, only 7-bit ASCII, so when I convert a string with high bit values the converter translates the character as a question mark instead of the actual byte for the character. ... Since strings are stored in Unicode in .Net I'm unable to simply take each char and cast it to a byte because it's liable to loose precision on non-English character sets. ... array in memory that always has the PETSCII value of the entire document and whenever a change event happens on my rich text box then I need to look at the new character, translate it, and then insert it into the byte array at the appropriate spot. ...
    (comp.sys.cbm)
  • Re: Engineers doing stupid things "just for fun".
    ... practical penalties for having constant strings in the source. ... you renamed the original source file ... converter. ... The .c file, to maintain readability, did conversions like: ...
    (comp.programming)
  • Re: VBScript String Left(string, int) equivalent in VB.NET ??
    ... Jens ... > qualify it with "Strings" since Left is also a form property). ... > Instant VB: C# to VB.NET Converter ...
    (microsoft.public.vsnet.general)