string - Using python and postgres, variables within execute function? -


i had question regarding usage of variables inside python function accesses postgresql server. example, following:

def delete():     cur.execute(     """delete potluck      name = var_1;""" 

however, if wanted update function take in variables var_1, how so?

for example, want function in form:

def delete(var_1):     cur.execute(     """delete potluck      name = %s;""", (var_1)) 

however, typing didn't work.

in addition, how in case when:

def delete(name, var_1):     cur.execute(     """delete potluck      %s = %s;""", (name, var_1)) 

where don't want "name" have quotation marks when inserted string?

any appreciated!

to pass identifiers use psycopg2.extensions.asis

from psycopg2.extensions import asis  def update(table_name, var_1, var_2):     cur.execute("""         update %s         set %s = 'y'         %s = 'john';         """,         (asis(table_name), asis(var_1), asis(var_2))     ) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -