How to do a LIKE query in SQLite (in Javascript) whilst passing in the search term as a parameter

这一生的挚爱 提交于 2021-01-29 19:08:41

问题


I am using React Native and am querying a SQLite database.

I wish to perform a like query, specifying the search term as a parameter.

The below works fine with the ? notation.

SELECT * from People WHERE Name = ?

This does not work or any similar variation I've tried:

SELECT * from People WHERE Name LIKE '%?%' 

Anyone know how to do this properly please? Please no interpolations or suggestions open to SQL injection.


回答1:


You can do string concatenation in SQLite:

SELECT * from People WHERE Name LIKE '%' || ? || '%' 

Another option is to concatenate the percent marks with the parameter in your javascript code before passing it to the query, so you can then do:

SELECT * from People WHERE Name LIKE ?


来源:https://stackoverflow.com/questions/59686415/how-to-do-a-like-query-in-sqlite-in-javascript-whilst-passing-in-the-search-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!