How to execute SELECT * LIKE statement with a placeholder in sqlite?

后端 未结 2 1871
面向向阳花
面向向阳花 2021-01-18 23:35

I\'ve got an argument tag and I perfomed this way:

cursor.execute(\"SELECT * FROM posts WHERE tags LIKE \'%?%\'\", (tag,))

but

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 00:34

    Remove the %:

    cursor.execute("SELECT * FROM posts WHERE tags LIKE ?", (tag,))
    

    This should format it as you wanted. For example, if tag == 'test' the full query would be:

    SELECT * FROM posts WHERE tags LIKE 'test'
    

提交回复
热议问题