Newbie Python question about strings with parameters: “%%s”?

I'm trying to figure out what the following line does exactly - specifically the %%s part?

cursor.execute('INSERT INTO mastertickets (%s, %s) VALUES (%%s, %%s)'%sourcedest, (self.tkt.id, n))

Any good mini-tutorial about string formatting and inserting variables into strings with Python?

Answers


The %% becomes a single %. This code is essentially doing two levels of string formatting. First the %sourcedest is executed to turn your code essentially into:

cursor.execute('INSERT INTO mastertickets (BLAH, FOO) VALUES (%s, %s)', (self.tkt.id, n))

then the db layer applies the parameters to the slots that are left.

The double-% is needed to get the db's slots passed through the first string formatting operation safely.


Need Your Help

How to stretch button all over the grid, xaml

c# xaml grid stackpanel

I have a problem with stretching the button all over the grid.

Access to one portlet from another

java file properties path portlet

I'm trying to write portlet, in which I want to create/edit some .properties file inside other portlet (I want to manipulate properties file responsible for translates from other portlet). I want t...