Converting Perl Web Report to Python

pmcgover at gmail.com pmcgover at gmail.com
Mon Sep 25 07:32:41 EDT 2006


Thanks again Dennis,
This should do what I want with additional flexibility...  I will
develop the code later this week.   During this excersize, I have come
to really appreciate Python over Perl.  I love the Python command line
interpreter that allowed me to pretest any code very quickly.  What
Python learning resource (book web link or both) do you recommend?
Pat

Dennis Lee Bieber wrote:
> On 24 Sep 2006 14:05:29 -0700, "pmcgover at gmail.com" <pmcgover at gmail.com>
> declaimed the following in comp.lang.python:
>
>
> > download and install the required MySQLdb module.  In any event, I
> > would like to know how to search and replace a given string in the sql
> > to a user supplied value before execution.
> >
> 	I would not even want to try to code that. I will mention that the
> default mode for MySQLdb is to use %s as the substitution field, and
> since both scripts used the same SQL file...
>
> > For example, lets say that your sql script looks like this:
> > > -=-=-=-=-=-=-=-=-=-		script1.sql
> > select name, URL, description, occurs
> > from comics
> > where URL like '%MyParam_1%'        # Note that "MyParam_1" represents
> > a "catch" string.
> > order by name;
> > > -=-=-=-=-=-=-=-=-=-
> >
>  	select name, URL, description, occurs
> 	from comics
> 	where URL like %s
> 	order by name;
>
> Then, on the cursor.execute() call:
>
> > > cr.execute(query)   #no parameters assumed
>
> 	cr.execute(query, ("%" + MyParam_1 + "%",))
>
> {MySQLdb /can/ work with a singleton for convenience, but the DB-API
> emphasizes a tuple be passed, hence the (... ,) to create a tuple}
>
> 	I'm presuming you do not require the "user" to enter the % wildcards
> on search terms.
>
> 	MySQLdb can also be fed with a dictionary (last time I looked, at
> least) so something like:
>
>  	select name, URL, description, occurs
> 	from comics
> 	where URL like %(MyParam_1)s or URL like %(MyParam_2)s
> 	order by name;
>
> and
>
> 	params = {}
> 	params["MyParam_1"] = "%" + somevariable + "%"
> 	params["MyParam_2"] = "%" + anotherstring + "%"
> 	cr.execute(query, params)
>
> may be possible (I've never used this mode, so no confirmation).
> --
> 	Wulfraed	Dennis Lee Bieber		KD6MOG
> 	wlfraed at ix.netcom.com		wulfraed at bestiaria.com
> 		HTTP://wlfraed.home.netcom.com/
> 	(Bestiaria Support Staff:		web-asst at bestiaria.com)
> 		HTTP://www.bestiaria.com/




More information about the Python-list mailing list