[Tutor] String representation of NULL (non type) values
Peter Otten
__peter__ at web.de
Tue Nov 5 17:37:56 CET 2013
Ulrich Goebel wrote:
> Hallo,
>
> from a SQLite database I get a value by SELECT s from... which normaly
> is a string, but can be the NULL value, wich means it is not defined. To
> put the value into a form (made by QT) I need a string representation.
>
> str(s) gives either the string itself (which is good) or "None" (which
> is not so good) in the case of NULL. Instead of "None" I would prefer an
> empty string "". How to get that?
>
> Possibly there is a build in function smart(s1, s2, s3,...) which
> returns the first s which is a useable string, or even "" if there isn't
> any string in the arguments?
coalesce(arg1, arg2, arg3, ...)
returns the first non-NULL argument; you'll get the empty string by
providing it as the last argument:
>>> db = sqlite3.connect(":memory:")
>>> cs = db.cursor()
>>> next(cs.execute("select coalesce('could be a field', '');"))
(u'could be a field',)
>>> next(cs.execute("select coalesce(NULL, '');"))
(u'',)
See
http://sqlite.org/lang_corefunc.html
More information about the Tutor
mailing list