Re: QuoteSQL

2006-09-29 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > In message <[EMAIL PROTECTED]>, LI wrote: > > execfile("QuoteSQL.py") > EscapeSQLWild(r"\%") >> '%' > SQLString("%" + EscapeSQLWild(r"\%") + "%") >> '"%%%"' > EscapeSQLWild(r"\%") == r"\\%" >> True > SQLString("%" +

Re: QuoteSQL

2006-09-28 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, LI wrote: execfile("QuoteSQL.py") EscapeSQLWild(r"\%") > '%' SQLString("%" + EscapeSQLWild(r"\%") + "%") > '"%%%"' EscapeSQLWild(r"\%") == r"\\%" > True SQLString("%" + EscapeSQLWild(r"\%") + "%") == r'"%%%"' > True With the

Re: QuoteSQL

2006-09-28 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, I wrote: > def EscapeSQLWild(Str) : > """escapes MySQL pattern wildcards in Str.""" > Result = [] > for Ch in str(Str) : > if Ch == "%" or Ch == "_" : > Result.append("\\") > #end if > R

Re: QuoteSQL

2006-09-28 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > In message <[EMAIL PROTECTED]>, Duncan Booth wrote: >> Deary me. Did you actually test out that bit of code before you >> posted it? > execfile("QuoteSQL.py") EscapeSQLWild(r"\%") > '%' SQLString("%" + EscapeSQLWild(r"\%") + "%")

Re: QuoteSQL

2006-09-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >> I'm assuming you mean, how would you get from a Python expression to a >> MySQL clause that looks like >> >> name like "%%%" >> >> (wildcard % followed by literal backslash \\ follo

Re: QuoteSQL

2006-09-27 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > I'm assuming you mean, how would you get from a Python expression to a > MySQL clause that looks like > > name like "%%%" > > (wildcard % followed by literal backslash \\ followed by literal > percent \% followed by wildcard %.) That's eas

Re: QuoteSQL

2006-09-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >>> You are still missing the point. I'm not talking about generating a >>> MySQL string literal, I'm talking about preventing wildcards >>> characters having their special meaning when using th

Re: QuoteSQL

2006-09-27 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >> You are still missing the point. I'm not talking about generating a >> MySQL string literal, I'm talking about preventing wildcards >> characters having their special meaning when using the string as a >> parameter in cursor.execute. > > But that'

Re: QuoteSQL

2006-09-27 Thread Ben
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > > > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > > > >> In message <[EMAIL PROTECTED]>, Duncan Booth > >> wrote: > >> > >>> Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >>> > def EscapeSQLWild(Str)

Re: QuoteSQL

2006-09-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >> In message <[EMAIL PROTECTED]>, Duncan Booth >> wrote: >> >>> Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >>> def EscapeSQLWild(Str) : """escapes MySQL pattern wild

Re: QuoteSQL

2006-09-27 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > In message <[EMAIL PROTECTED]>, Duncan Booth > wrote: > >> Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >> >>> def EscapeSQLWild(Str) : >>> """escapes MySQL pattern wildcards in Str.""" >>> Result = [] >>> for Ch in

Re: QuoteSQL

2006-09-27 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >> def EscapeSQLWild(Str) : >> """escapes MySQL pattern wildcards in Str.""" >> Result = [] >> for Ch in str(Str) : >> if Ch == "%" or Ch == "_" : >>

Re: QuoteSQL

2006-09-27 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > def EscapeSQLWild(Str) : > """escapes MySQL pattern wildcards in Str.""" > Result = [] > for Ch in str(Str) : > if Ch == "%" or Ch == "_" : > Result.append("\\") > #end if >

Re: QuoteSQL

2006-09-26 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Steve > Holden wrote: [...] >>Sadly your assertions alone fail to convince. Perhaps you could provide >>a concrete example? > > > Sorry, that turned out to be wrong. You do in fact need to escape the > escapes on wildcards. Thank you.

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Steve Holden wrote: > Lawrence D'Oliveiro wrote: > >> In message <[EMAIL PROTECTED]>, Duncan Booth wrote: >> >>>In particular it currently turns newlines in backslash followed by n >>>which (since MySQL ignores the extra backslash escape) is equivalent to >>>turnin

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Steve Holden wrote: > Lawrence D'Oliveiro wrote: >> In message <[EMAIL PROTECTED]>, Steve >> Holden wrote: >> >> >>>Lawrence D'Oliveiro wrote: >>> In message <[EMAIL PROTECTED]>, Steve Holden wrote: >When you use the DB API correctly and

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Sybren Stuvel wrote: > Tim Chase enlightened us with: >> >>> cur.execute("select * from people where last_name in (%s)" % >> ','.join('%s' for i in xrange(len(t))), >> t) > > But since the value of 'i' isn't used at all, it can be abbreviated > to: > cur.exec

Re: QuoteSQL

2006-09-26 Thread Fredrik Lundh
Dennis Lee Bieber wrote: > So.. In the case of MySQLdb, one can modify the dictionary of quote > converters... Seems like one could create a "class" for "LIKE" wildcard > strings, rather than just pass plain strings, and add that "type" to the > list of quote converters. That way, one's cust

Re: QuoteSQL

2006-09-26 Thread Fredrik Lundh
Sybren Stuvel wrote: > I didn't care anything about all that. I just found the way I wrote it > somewhat easier to read. absolutely. -- http://mail.python.org/mailman/listinfo/python-list

Re: QuoteSQL

2006-09-26 Thread Sybren Stuvel
Fredrik Lundh enlightened us with: > and now we're waiting for the "['%s']*len(t)" vs. "'%s' for i in t" > benchmarks (and the "consistency is more important than efficiency" > and "creating extra objects is conceptually wrong" followups, and > the "it depends on what you mean by" followups to the

Re: QuoteSQL

2006-09-26 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > > >>However, your QuoteSQL messes up every time because it wraps double >>quotes round the whole string, so it isn't suitable for use with >>parameterised queries at al

Re: QuoteSQL

2006-09-26 Thread Sybren Stuvel
Tim Chase enlightened us with: > >>> cur.execute("select * from people where last_name in (%s)" % > ','.join('%s' for i in xrange(len(t))), > t) But since the value of 'i' isn't used at all, it can be abbreviated to: >>> cur.execute("select * from people where last_name in (%s)" %

Re: QuoteSQL

2006-09-26 Thread Tim Chase
> "Need" is a strong word unless something like the > following doesn't work for some reason: > > cur.execute("select * from people where last_name in > (%s,%s,%s)", (name1, name2, name3) ) Which could be nicely generalized to something like >>> t = (name1, name2, name3) >>> cur.execute("s

Re: QuoteSQL

2006-09-26 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Steve > Holden wrote: > > >>Lawrence D'Oliveiro wrote: >> >>>In message <[EMAIL PROTECTED]>, Steve >>>Holden wrote: >>> >>> >>> When you use the DB API correctly and paramterise your queries you still need to quote wildcards in

Re: QuoteSQL

2006-09-26 Thread Fredrik Lundh
Sybren Stuvel wrote: > >>> cur.execute("select * from people where last_name in (%s)" % > >>> ','.join('%s' for i in t), t) and now we're waiting for the "['%s']*len(t)" vs. "'%s' for i in t" benchmarks (and the "consistency is more important than efficiency" and "creating extra objects is con

Re: QuoteSQL

2006-09-26 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > > >>However, your QuoteSQL messes up every time because it wraps double >>quotes round the whole string, so it isn't suitable for use with >>parameterised queries at al

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Sybren Stuvel wrote: > Lawrence D'Oliveiro enlightened us with: >> You're proposing two separate functions: >> >> 1) quoting of non-wildcard specials >> 2) quoting of wildcard specials > > I'm suggesting functions based on the role of the string they need t

Re: QuoteSQL

2006-09-26 Thread Carsten Haese
On Tue, 2006-09-26 at 07:08, Lawrence D'Oliveiro wrote: > So yes, there should be two separate functions, one for escaping > non-wildcard specials, and one for escaping wildcards. > > > You only need the first one, since every database interface that > > follows PEP 249. > > You still need the se

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > However, your QuoteSQL messes up every time because it wraps double > quotes round the whole string, so it isn't suitable for use with > parameterised queries at all. If you care to modify it to work in that > situat

Re: QuoteSQL

2006-09-26 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > Because quoting the wildcards introduces backslash specials before > each wildcard. Quoting non-wildcards then causes those backslashes > to be doubled, which means they escape themselves instead of the > wildcards. I don't know about other DBMSes, but in

Re: QuoteSQL

2006-09-26 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > You're proposing two separate functions: > > 1) quoting of non-wildcard specials > 2) quoting of wildcard specials I'm suggesting functions based on the role of the string they need to escape, not the characters in that string. 1) Quoting of

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Sybren Stuvel wrote: > Lawrence D'Oliveiro enlightened us with: >> The trouble with this is that, instead of offering extra functionality, >> it leaves the door open to making two stupid mistakes: >> >> 2) quoting of wildcards BEFORE quoting of non-wildcards >

Re: QuoteSQL

2006-09-26 Thread Duncan Booth
> matching a newline. > > And how would you get my QuoteSQL routine, as written, to make the same > mistake you did? If you think I made a mistake I'm afraid you'll have to tell me what it was. I'm unable to read your mind. However, your QuoteSQL messes up every time b

Re: QuoteSQL

2006-09-26 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > The trouble with this is that, instead of offering extra functionality, it > leaves the door open to making two stupid mistakes: > > 2) quoting of wildcards BEFORE quoting of non-wildcards Why is this a "stupid" mistake in your view? Please explain th

Re: QuoteSQL

2006-09-26 Thread Fredrik Lundh
Sybren Stuvel wrote: > I'm suggesting functions based on the role of the string they need to > escape, not the characters in that string. > > 1) Quoting of wildcard strings for a query using LIKE etc. > 2) Quoting of values for putting into queries. it's actually quite amusing that some

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > The spurious escaping of the apostrophe does no harm, but spuriously > escaping a newline makes the select match the letter 'n' insteal of > matching a newline. And how would you get my QuoteSQL routine, as written,

Re: QuoteSQL

2006-09-26 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Steve Holden wrote: > Lawrence D'Oliveiro wrote: >> >> In message <[EMAIL PROTECTED]>, Steve >> Holden wrote: >> >> >>>When you use the DB API correctly and paramterise your queries you still >>>need to quote wildcards in search arguments, but you absolutely >>>sh

Re: QuoteSQL

2006-09-26 Thread Fredrik Lundh
Steve Holden wrote: > Ah, so your quoting function will deduce the context in which arguments > intended for parameter substitution in the query will be used? Or are > you suggesting that it's unwise to rely on autoquoted parameters? That > could have a serious impact on the efficiency of some

Re: QuoteSQL

2006-09-25 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Steve > Holden wrote: > > >>When you use the DB API correctly and paramterise your queries you still >>need to quote wildcards in search arguments, but you absolutely >>shouldn't quote the other SQL specials. >> >>That's what parameteri

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Jon Ribbens wrote: > In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro wrote: >> You're proposing two separate functions: >> >> 1) quoting of non-wildcard specials >> 2) quoting of wildcard specials > > Lawrence, you're wrong... Did I misunderstand what

Re: QuoteSQL

2006-09-25 Thread Jon Ribbens
In article <[EMAIL PROTECTED]>, Lawrence D'Oliveiro wrote: > You're proposing two separate functions: > > 1) quoting of non-wildcard specials > 2) quoting of wildcard specials Lawrence, you're wrong in this thread for the same reason you were right in the cgi.escape thread. Escaping gener

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Steve Holden wrote: > When you use the DB API correctly and paramterise your queries you still > need to quote wildcards in search arguments, but you absolutely > shouldn't quote the other SQL specials. > > That's what parameterised queries are for on the first pla

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
g of non-wildcard specials only 2) quoting of both non-wildcard and wildcard specials Or: * A single function that performs either of the above two operations, depending on a boolean flag--which is what QuoteSQL does. I guess I don't have a strong preference for which way we do

Re: QuoteSQL

2006-09-25 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: >> An escaping function should be small and not do all kinds of >> escaping for different situations at once. > > Even when the two situations are related? Yup, even then. Different situations need different escaping functions. Sybren -- Sybren Stüvel St

Re: QuoteSQL

2006-09-25 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >> Indeed. An escaping function should be small and not do all kinds of >> escaping for different situations at once. > > Look at it this way: there is _no_ case where you need escaping of > wildcards without also escaping other specials. You need t

Re: QuoteSQL

2006-09-25 Thread Steve Holden
looked for literal percents. >> >>But of course. >> >> >>>This could be an argument for having a utility function to escape >>>the wildcards for this sort of situation, but certainly not an >>>argument for his proposed QuoteSQL. >> >>Indeed

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
This could be an argument for having a utility function to escape >> the wildcards for this sort of situation, but certainly not an >> argument for his proposed QuoteSQL. > > Indeed. An escaping function should be small and not do all kinds of > escaping for different situatio

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
This could be an argument for having a utility function to escape >> the wildcards for this sort of situation, but certainly not an >> argument for his proposed QuoteSQL. > > Indeed. An escaping function should be small and not do all kinds of > escaping for different situ

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Duncan Booth wrote: > Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > >>> This could be an argument for having a utility function to escape the >>> wildcards for this sort of situation, but certainly not an argument for

Re: QuoteSQL

2006-09-25 Thread Sybren Stuvel
sort of situation, but certainly not an > argument for his proposed QuoteSQL. Indeed. An escaping function should be small and not do all kinds of escaping for different situations at once. Sybren -- Sybren Stüvel Stüvel IT - http://www.stuvel.eu/ -- http://mail.python.org/mailman/listinfo/python-list

Re: QuoteSQL

2006-09-25 Thread Duncan Booth
Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: >> This could be an argument for having a utility function to escape the >> wildcards for this sort of situation, but certainly not an argument for >> his proposed QuoteSQL. > > Why not? That is exactly one of the

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
looked for literal percents. > > This could be an argument for having a utility function to escape the > wildcards for this sort of situation, but certainly not an argument for > his proposed QuoteSQL. Why not? That is exactly one of the options my QuoteSQL offers. -- http://mail.python.org/mailman/listinfo/python-list

Re: QuoteSQL

2006-09-25 Thread Duncan Booth
ncluding all sorts of quotes, newlines, backslashes etc. in the name. > I think his point was that any '%' characters inside name act like wildcards whereas his version looked for literal percents. This could be an argument for having a utility function to escape the wildcards for this

Re: QuoteSQL

2006-09-25 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: >> Wouldn't this be a whole lot better? >> >> cursor.execute( >> "select * from details where person_name like ?", >> '%' + name + '%' >> ) > > No. Can you figure out why? Ok, should have tested it better. This works fine on my machine, though: cu

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Sybren Stuvel wrote: > Lawrence D'Oliveiro enlightened us with: >> "select * from details where person_name like" >> " concat(\"%%\", %s, \"%%\")" \ >> % \

Re: QuoteSQL

2006-09-25 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > "select * from details where person_name like" > " concat(\"%%\", %s, \"%%\")" \ > % \ > QuoteSQL(name, True) Wouldn't this be a whole lot better? cur

Re: QuoteSQL

2006-09-25 Thread Lawrence D'Oliveiro
* from details where person_name like" " concat(\"%%\", %s, \"%%\")" \ % \ QuoteSQL(name, True) -- http://mail.python.org/mailman/listinfo/python-list

Re: QuoteSQL

2006-09-24 Thread Steve Holden
Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Sybren Stuvel > wrote: > > >>Lawrence D'Oliveiro enlightened us with: >> >>>Why doesn't MySQLdb provide a function like this: >> >>Because generally you're able to pass newlines and the like just fine. >>You can even store binary data i

Re: QuoteSQL

2006-09-24 Thread Anders J. Munch
Lawrence D'Oliveiro wrote: >>> elif Ch == "'" or Ch == "\"" or Ch == "\\" : >>> Ch = "\\" + Ch >> Always sad to see an SQL DBMS willfully violate the SQL standard. > > Why is that a violation of SQL? Taking another look, I might be wrong: Your code uses double quotes, and sin

Re: QuoteSQL

2006-09-24 Thread Anders J. Munch
Robert Kern wrote: > Anders J. Munch wrote: > >> Always sad to see an SQL DBMS willfully violate the SQL standard. > > You must be a constantly depressed person, then. :-) Nah, I just look the other way most of the time *g* - Anders -- http://mail.python.org/mailman/listinfo/python-list

Re: QuoteSQL

2006-09-24 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > Yes, I have done blobs. Still need a quoting function for the > specials, though. Why? What makes your data so different from mine? I can store newlines and the likes just fine in a regular text field. Sybren -- Sybren Stüvel Stüvel IT - http://www.st

Re: QuoteSQL

2006-09-24 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Sybren Stuvel wrote: > Lawrence D'Oliveiro enlightened us with: >> Why doesn't MySQLdb provide a function like this: > > Because generally you're able to pass newlines and the like just fine. > You can even store binary data into a BLOB column. Yes, I have done bl

Re: QuoteSQL

2006-09-24 Thread Sybren Stuvel
Lawrence D'Oliveiro enlightened us with: > Why doesn't MySQLdb provide a function like this: Because generally you're able to pass newlines and the like just fine. You can even store binary data into a BLOB column. Sybren -- Sybren Stüvel Stüvel IT - http://www.stuvel.eu/ -- http://mail.pytho

Re: QuoteSQL

2006-09-23 Thread Robert Kern
Anders J. Munch wrote: > Always sad to see an SQL DBMS willfully violate the SQL standard. You must be a constantly depressed person, then. :-) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpr

Re: QuoteSQL

2006-09-23 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Anders J. Munch wrote: > Lawrence D'Oliveiro wrote: >> Why doesn't MySQLdb provide a function like this: >> >> def QuoteSQL(Str, DoWild) : >> """returns a MySQL string literal which evaluates to S

Re: QuoteSQL

2006-09-23 Thread Anders J. Munch
Lawrence D'Oliveiro wrote: > Why doesn't MySQLdb provide a function like this: > > def QuoteSQL(Str, DoWild) : > """returns a MySQL string literal which evaluates to Str. Needed > for those times when MySQLdb's automatic quoting isn't good

Re: QuoteSQL

2006-09-23 Thread John Machin
Lawrence D'Oliveiro wrote: > Why doesn't MySQLdb provide a function like this: Because the author has read PEP 8? -- http://mail.python.org/mailman/listinfo/python-list

QuoteSQL

2006-09-23 Thread Lawrence D'Oliveiro
Why doesn't MySQLdb provide a function like this: def QuoteSQL(Str, DoWild) : """returns a MySQL string literal which evaluates to Str. Needed for those times when MySQLdb's automatic quoting isn't good enough.""" Result = []