converting sqlite return values

Jason Drew jasondrew72 at gmail.com
Mon Feb 20 17:59:14 EST 2006


Hi,

You can use the built-in function "eval" to return how Python evaluates
your string. For example:
>>> eval( '(1,2,3,4)' )
(1, 2, 3, 4)

In other words, eval will take your string that looks like a tuple, and
return an actual tuple object.

Note that the 'u' prefix in your string will cause an error if you pass
it to eval, so you should drop that, e.g.:
>>> utuple = 'u(1,2,3,4)'
>>> eval( utuple[1:] )
(1, 2, 3, 4)

In general, though, converting your strings/tuples back and forth like
this might not be the best idea, depending on the situation. If the
numbers represent consistent items, like (price, tax, code, quantity),
then you would do better to use a field for each item in your database
and insert/fetch the numbers appropriately.

Storing whole Python objects in single database fields isn't unheard
of, but in general you should only do it when you really need to do it.
When you do, there are various Python modules to help, though I haven't
used this approach much myself.

Jason




More information about the Python-list mailing list