[Tutor] mysql formatting

Rick Pasotto rick at niof.net
Wed Nov 3 21:16:10 CET 2004


On Wed, Nov 03, 2004 at 11:30:37AM -0800, Marilyn Davis wrote:
> 
> Yes, Danny is right that the database name must be hard-coded:
> 
>      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)

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


>      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)
> 
> 
>      self.cursor.execute("update doorman set status = "%s" where in_id = %s and 
> out_address like "%%%s%%"", MOVED, 60, courier-imap-admin at lists.sourceforge.net)
>                                                                                                  ^
> SyntaxError: invalid syntax

Of course.

        .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]

'parameters' must be a *sequence* (list or tuple) or *mapping* (dictionary).
You used three individual items. This should work:

my_connection.execute_mysql(s, (new_status, inside_id, out_address))

Note the extra parentheses to make a tuple.

-- 
"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]
    Rick Pasotto    rick at niof.net    http://www.niof.net


More information about the Tutor mailing list