How do I escape % in Knex where like query?

后端 未结 6 1445
挽巷
挽巷 2021-01-18 00:09

I\'m using knex to generate my SQL queries. In knex documentation, it shows this

knex(\'users\').where(\'columnName\', \'like\', \'         


        
6条回答
  •  盖世英雄少女心
    2021-01-18 01:09

    For this case I use

    rather string interpolation from es6 (safe version)

    knex('table').where('description', 'like', `%${term}%`)
    

    or ?? parameter binding

    knex('table').whereRaw('description like \'%??%\'', [term])
    

    But in the first case, you must be 100% sure that term is valid, because of the possibility of SQL injection.

提交回复
热议问题