Re: LIKE '%something%' custom queries
[EMAIL PROTECTED] wrote: > I'm reimplementing an in-database tree system and I'm using custom SQL > queries. I want to use % in LIKE but Django doesn't let me. > > The code: > ## > def getBranch(table_name, parent_depth, parent_cutLevel, max_depth): > cursor = connection.cursor() > cursor.execute("SELECT *, INSTR(level,'0')-1 AS depth, > INSTR(level,'0')-1 - "+ parent_depth +" AS relativeDepth FROM " + > table_name + " WHERE level LIKE '"+ parent_cutLevel +"%' AND > INSTR(level,'0')-1 <= ("+ max_depth +"+"+ parent_depth +") ORDER BY > level") > row = cursor.fetchall() > return row > ## The error says that Python tries to replace '%' with parameters but there aren't any. To escape them duplicate them: " WHERE level LIKE '"+ parent_cutLevel +"%%' AND ... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: LIKE '%something%' custom queries
On 23/05/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I'm reimplementing an in-database tree system and I'm using custom SQL > queries. I want to use % in LIKE but Django doesn't let me. > > The code: > ## > def getBranch(table_name, parent_depth, parent_cutLevel, max_depth): > cursor = connection.cursor() > cursor.execute("SELECT *, INSTR(level,'0')-1 AS depth, > INSTR(level,'0')-1 - "+ parent_depth +" AS relativeDepth FROM " + > table_name + " WHERE level LIKE '"+ parent_cutLevel +"%' AND > INSTR(level,'0')-1 <= ("+ max_depth +"+"+ parent_depth +") ORDER BY > level") > row = cursor.fetchall() > return row > ## > > The exception: > ## > not enough arguments for format string > Exception Location: /usr/lib64/python2.4/site-packages/django/db/ > backends/util.py in execute, line 19 > ## > I can "print" the query string, but can't make the query as cursor > code uses %letters... Is there a solution for this. ( \ doesn't work, > % in variable also) > Umm... the "%" symbol has a special meaning in a string, which is to interpolate it and fill it with arguments from a provided list. In a cursor it's a variable placeholder used for prepared (or normal) queries. If you want it literal, you must escape it properly. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: LIKE '%something%' custom queries
Don't put the % inside of " characters, or Python will attempt to evaluate it out. Alternately, escape it so it doesn't eval. I generally form up my custom SQL strings outside of the execute() method to make debugging this kind of thing a tad easier. -joe On 5/23/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I'm reimplementing an in-database tree system and I'm using custom SQL > queries. I want to use % in LIKE but Django doesn't let me. > > The code: > ## > def getBranch(table_name, parent_depth, parent_cutLevel, max_depth): > cursor = connection.cursor() > cursor.execute("SELECT *, INSTR(level,'0')-1 AS depth, > INSTR(level,'0')-1 - "+ parent_depth +" AS relativeDepth FROM " + > table_name + " WHERE level LIKE '"+ parent_cutLevel +"%' AND > INSTR(level,'0')-1 <= ("+ max_depth +"+"+ parent_depth +") ORDER BY > level") > row = cursor.fetchall() > return row > ## > > The exception: > ## > not enough arguments for format string > Exception Location: /usr/lib64/python2.4/site-packages/django/db/ > backends/util.py in execute, line 19 > ## > I can "print" the query string, but can't make the query as cursor > code uses %letters... Is there a solution for this. ( \ doesn't work, > % in variable also) > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
LIKE '%something%' custom queries
I'm reimplementing an in-database tree system and I'm using custom SQL queries. I want to use % in LIKE but Django doesn't let me. The code: ## def getBranch(table_name, parent_depth, parent_cutLevel, max_depth): cursor = connection.cursor() cursor.execute("SELECT *, INSTR(level,'0')-1 AS depth, INSTR(level,'0')-1 - "+ parent_depth +" AS relativeDepth FROM " + table_name + " WHERE level LIKE '"+ parent_cutLevel +"%' AND INSTR(level,'0')-1 <= ("+ max_depth +"+"+ parent_depth +") ORDER BY level") row = cursor.fetchall() return row ## The exception: ## not enough arguments for format string Exception Location: /usr/lib64/python2.4/site-packages/django/db/ backends/util.py in execute, line 19 ## I can "print" the query string, but can't make the query as cursor code uses %letters... Is there a solution for this. ( \ doesn't work, % in variable also) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---