[Tutor] mysql formatting

Marilyn Davis marilyn at deliberate.com
Wed Nov 3 22:23:05 CET 2004


On Wed, 3 Nov 2004, Rick Pasotto wrote:

> update %s set status = "%s" where in_id = %s and out_address like %s
> """ % ('doorman','%s','%s','%%%s%%')

I see.  But I'm happy hardcoding the name.  That was a silly thing
anyway.  

> 
>         .execute(operation[,parameters]) 
>           
>             Prepare and execute a database operation (query or
>             command).  Parameters may be provided as sequence or
>             mapping and will be bound to variables in the operation.
>             Variables are specified in a database-specific notation
>             (see the module's paramstyle attribute for details). [5]

Rick!  Where did you get this from?  help(MySQLdb) doesn't tell me
this.  What's [5]?  What's "paramstyle attribute"?  I'm missing
something big about documentation, and that's for sure!

Ok.  Not unpacking the tuple means that my method is now:

     def execute_mysql(self, this, args):
         try:
             self.cursor.execute(this, args)

And this call works!

     s = """update doorman set status = %s where in_id = %s and out_address like %s"""
     my_connection.execute_mysql(s, (new_status, inside_id, '%'+ out_address+'%'))

It produces:

update doorman set status = 'MOVED' where in_id = '60' and out_address
like '%courier-imap-admin at lists.sourceforge.net%';

I wouldn't have guessed that in_id = '60' would work since it is an
integer, but it does.

It all works!  

Whew!

"Yes, there is Nirvanah; it is in leading your sheep to a green pasture, 
 and in putting your child to sleep, and in writing the last line of your 
 poem." -- Kahlil Gibran (1883-1931) [Sand and Foam]

I guess he didn't know about getting a line of code to work.  :^)

Thank you soooo much.  I just love it when things make sense.

Marilyn










More information about the Tutor mailing list