On Sat, 2009-08-01 at 19:29 -0700, David wrote: > Hi, > > When I tested sql of MySQL in Python interactive shell, I got an > error of "not enough arguments for format string". I checked the > arguments however I could not find anything wrong. > > Can anybody give me a clue? > > Thanks so much. > > > >>> sql = Template('select dimension1 ,adddate(current_date,-1),current_date, > >>> ifnull(sum(case when date_format(creation_date,\'%Y-%m-%d\') = > >>> adddate(current_date,-1) then ifnull($ss_1,0) end),0) as \'PreviousDay\', > >>> ifnull(sum(case when date_format(creation_date,\'%Y-%m-%d\') = > >>> current_date then ifnull($ss_2,0) end),0) as \'Today\' from > >>> message_alert where dataset_id = $ss_3 and dimension1 in ($ss_4) and > >>> hour(creation_date) = hour(now())-1 group by dimension1 having > >>> (ifnull(sum(case when date_format(creation_date,\'%Y-%m-%d\') = > >>> current_date then ifnull($ss_5,0) end),0) / ifnull(sum(case when > >>> date_format(creation_date,\'%Y-%m-%d\') = adddate(current_date,-1) then > >>> ifnull($ss_6,0) end),0)) $ss_7 $ss_8') > > >>> > >>> sql = sql.substitute(ss_1='metric1', ss_2='metric1', ss_3=2, > >>> ss_4='557796,558069,558230', ss_5='metric1', ss_6='metric1', ss_7='>=', > >>> ss_8=0.05)
You seem to be trying to create raw SQL using Django's template system. Please don't do that. It's like trying to use a shovel to hammer in a nail, but less effective. You are also using something other than Django's templating system since django.templates.Template does not have a substitute() method. And Python format markers in strings are things start with "%", not "$", so "ss_1" and friends aren't variables in the string. If you are trying to create raw SQL, write it out as a string and pass the parameters in to the execute() call. Do not substitute them into the string first, since that is the easiest way to introduce SQL injection attacks. If you need more information about how to use cursor.execute(), have a read of PEP 249. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---