Python String Formats with SQL Wildcards and LIKE

后端 未结 6 904
情深已故
情深已故 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条回答
  •  Happy的楠姐
    2020-11-30 11:30

    We could try escaping the percentage character by doubling them like this:

    query_to_get_user_name = """ 
    SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN tag 
    ON user.id = tag.userId 
    WHERE user.username LIKE '%%%s%%' """ % (user_name,) 
    
    cursor.execute(query_to_get_user_name)
    

提交回复
热议问题