简体   繁体   中英

How to use wildcards with SQLite paramaters?

I'm having trouble figuring out how to use wildcards with parameters in SQLite.

Here's the code I'm trying to get working:

@GamePara='Desert';
SELECT
    GameName
FROM Games
WHERE GameName LIKE '%' + @GamePara + '%';

This code returns no results. It is supposed to return all fields which include the string 'Desert' in GameName .

If I replace the parameter with the desired value directly, as shown below, I am able to get the desired results.

SELECT
    GameName
FROM Games
WHERE GameName LIKE '%Desert%';

I've not had much experience with SQL parameters in general, so it's possible that my issue could exist with other versions of SQL as well.

MODERATOR EDIT: Note: It is very bad habit to delete question after getting correct answer.

USER EDIT: Sorry, I hadn't seen that the question had been answered when I deleted it (I hadn't refreshed the page). I deleted the question because I realised that I asked the wrong question. The issue that I was having was that I couldn't set parameters the same way as in MySQL. ( @GamePara='Desert'; returned the message Error: near "@GamePara": syntax error ). I had tried the || concatenation, but the parameter setting was the issue.

Use || for string concatenation:

The || operator is "concatenate" - it joins together the two strings of its operands.

@GamePara='Desert';
SELECT
    GameName
FROM Games
WHERE GameName LIKE '%' || @GamePara || '%';

Compare:

SELECT 'a' + 'b', 'a' || 'b'
-- 0 vs ab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM