logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: LIKE and %% problems...i think: msg#00042

Subject: Re: LIKE and %% problems...i think
Bram wrote:
Andreas Degert wrote:
Bram <bram-lists-gidg7/o9dPRAfugRpC6u6w@xxxxxxxxxxxxxxxx> writes:
  
Hello all,

I am using psycopg2, and I am trying to build some search functions
for my database in python. I am using string formatting to prepare the
sql string like so:

sqlstr = "SELECT * FROM assetattribs WHERE attrib_val LIKE '%%%s%%';"
% (querystring)
    

I think you forgot the trailing ",", try again with

... % (querystring,)

  
Nope, the comma is optional, as with any tuple, list, or dict unless of course there is more than one item in the tuple, list, or dict.

That's not true.  Well, not exactly.  The construct (querystring) is NOT a one-element tuple.  It is a scalar value.  Consider this _expression_:

    j = 3
    i = (j+j) * 4

Clearly, you want i to be 24, not (6,6,6,6), which is what you'd get if that were seen as a one-element tuple.  Andreas is partly right; to get a one-element tuple, you HAVE to use a comma, as in (querystring,).  And, if you are passing values to the psycopg execute method, you absolutely need it to be a tuple, so the comma is required.

However, the simple % operator, which you were using, happens to accept scalars as well as tuples:

    print "%d" % 3
    print "%d" % (3)
-- 
Tim Roberts, timr-EQQNi8F+HVEAvxtiuMwx3w@xxxxxxxxxxxxxxxx
Providenza & Boekelheide, Inc.
_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/+sw@xxxxxxxxxxxxxxxx
http://lists.initd.org/mailman/listinfo/psycopg
<Prev in Thread] Current Thread [Next in Thread>