Python String Formats with SQL Wildcards and LIKE

后端 未结 6 915
情深已故
情深已故 2020-11-30 11:04

I\'m having a hard time getting some sql in python to correctly go through MySQLdb. It\'s pythons string formatting that is killing me.

My sql statement is using the

6条回答
  •  忘掉有多难
    2020-11-30 11:49

    It's not about string formatting but the problem is how queries should be executed according to db operations requirements in Python (PEP 249)

    try something like this:

    sql = "SELECT column FROM table WHERE col1=%s AND col2=%s" 
    params = (col1_value, col2_value)
    cursor.execute(sql, params)
    

    here are some examples for psycog2 where you have some explanations that should also be valid for mysql (mysqldb also follows PEP249 dba api guidance 2.0: here are examples for mysqldb)

提交回复
热议问题