blob problems in pysqlite

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Apr 27 04:40:29 EDT 2006


[aldonnelley at gmail.com]

| I'm trying to use cPickle.loads(data) (code attached), and I get a:
| "TypeError: loads() argument 1 must be string, not list"
| 
| Is there a workaround for this? I've tried converting the 
| List of files
| to a String before cPickling it, but the same result (expected).

OK, This one looks more straightforward. The result of fetchall ()
is a list of tuples (even if it is a list of one tuple and even if
that tuple has only one element). This means that KnownFilesResult 
is a list. cPickle.loads wants a string. You can, obviously, convert 
the list to a string, but that would only result in something like 
"[(blah,), (blah,)]" which is clearly not what you want.

Depending on what you think you're going to get back, you
either need to use fetchone in place of fetchall, which
will return one tuple -- in this case containing one buffer 
-- or you need to iterate over the list, or unpack it, again 
depending on your expectation of the data.

Here, something like this fragment should work (untested):

.
.
# Iterate over the result set and unpack
#  each tuple into its elements.

KnownFilesResult = c.fetchall ()
for row in KnownFilesResult:
  (pickled_result,) = row
  unpickled_result = cPickle.loads (pickled_result)
  # do things with unpickled result
  
| ps. Tim: Your clean take on my code made me realise that I 
| had stupidly included quotes in the URL argument passed to 
| sqlite ... It's the little things that matter...

By the way, it's a lot easier for others (like me) to help
if you post a self-contained code sample. In both cases,
while your explanation has been lucid enough for me to
see / guess what's going one, I've had to add bits to
your posted code so that they work. It helps if you
add enough to the top of the code to -- in this case --
create the table you're using so that all anyone has
to do is to cut-and-paste the code into an interpreter.

(I realise that I haven't actually done this in return
this time, but that's partly because it might obscure
the point I'm trying to make and partly because it's
a drop-in replacement for your own code).

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list