为什么在我的应用程序中没有返回任何行,而在 DBBrowser 中有相同的查询 returns 行?

Why are no rows returned in my app, whereas the same query returns rows in DBBrowser?

通过对我的 SQLite 数据库的这个简单查询:

SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors,  screenwriters 
FROM MOVIES_SINGLETABLE 
WHERE IMDBRating >= @IMDBMinRating 
ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000

...我得到记录:

但过滤更多:

SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors, screenwriters 
FROM MOVIES_SINGLETABLE 
WHERE IMDBRating >= @IMDBMinRating 
AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
AND (genres LIKE '%@genre1%' OR genres LIKE '%@genre2%') 
AND (MPAARating = '@mpaaRating1') 
ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000

...我什么也没得到(是的,所有这些参数都分配了适当的值,即:5.0、'1958'、'2019'、'Drama'、'Western'、和 'PG'):

然而,使用相同的查询,我 获取记录 return 在 DBBrowser 中编辑:

这是怎么回事?为什么相同的查询 return 在我的应用程序中什么也没有,但来自 DBBrowser 的东西却很多?

更新

应大家的要求,下面是参数的分配方式(genresSelected 是一个字符串列表):

if (GenresFiltered())
{
    if (genresSelected.Count > 0)
    {
        cmd.Parameters.AddWithValue("@genre1", genresSelected[0].ToString());
    }
    if (genresSelected.Count > 1)
    {
        cmd.Parameters.AddWithValue("@genre2", genresSelected[1].ToString());
    }
    . . .

...在分配参数之前,查询字符串如下所示:

"SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors, screenwriters FROM MOVIES_SINGLETABLE WHERE IMDBRating >= @IMDBMinRating AND YearReleased BETWEEN @EarliestYear AND @LatestYear AND (genres LIKE '%@genre1%' OR genres LIKE '%@genre2%' OR genres LIKE '%@genre3%' OR genres LIKE '%@genre4%' OR genres LIKE '%@genre5%' OR genres LIKE '%@genre6%' OR genres LIKE '%@genre7%' OR genres LIKE '%@genre8%' OR genres LIKE '%@genre9%') AND (MPAARating LIKE '%@mpaaRating1%') ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000"

更新 2

现在的最终查询查询字符串是(去掉单引号):

SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors, screenwriters FROM MOVIES_SINGLETABLE WHERE IMDBRating >= @IMDBMinRating AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) AND (genres LIKE @genre1 OR genres LIKE @genre2) AND (MPAARating = @mpaaRating1) ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000

我尝试了以下所有排列,其中 none return 任何数据:

  1. "AND (genres = @genre1 OR genres = @genre2) "

  2. "AND ((genres = @genre1) OR (genres = @genre2)) "

  3. "AND ((genres LIKE '%' || @genre1 || '%') OR (genres LIKE '%' || @genre2 || '%')) "

  4. AND (genres LIKE '%'+@genre1+'%' OR genres LIKE '%'+@genre2+'%')

  5. AND (genres LIKE @genre1 OR genres LIKE @genre2) -与:

    cmd.Parameters.AddWithValue("@genre1",string.Format("%{0}%", genresSelected[0].ToString())); cmd.Parameters.AddWithValue("@genre2",string.Format("%{0}%", genresSelected1.ToString()));

更新 3

尽我所能结合 forpas 的想法,我能够在 DBBrowser 中获得记录 returned,但还没有在实际的应用程序中。注释掉的行和下面用于过滤流派的现有行都有效。如果没有包含值的单引号,我会收到一条关于“Western”不是列名的错误消息,但它周围的单引号有效:

所以我已经尝试了以下所有排列,并且 none 其中 return 任何记录:

// @genre1 and @genre2 not surrounded by single quotes
return "AND ((genres LIKE '%' || @genre1 || '%') OR (genres LIKE '%' || @genre2 || '%')) "; 

// @genre1 and @genre2 surrounded by single quotes
return "AND ((genres LIKE '%' || '@genre1' || '%') OR (genres LIKE '%' || '@genre2' || '%')) "; 

// @genre1 and @genre2 not surrounded by single quotes, string prepended with "@"
return @"AND ((genres LIKE '%' || @genre1 || '%') OR (genres LIKE '%' || @genre2 || '%')) "; 

// @genre1 and @genre2 surrounded by single quotes, string prepended with "@"
return @"AND ((genres LIKE '%' || '@genre1' || '%') OR (genres LIKE '%' || '@genre2' || '%')) ";             

接下来的问题是,我想:我如何在 C# 中生成与 在 DBBrowser 中工作的 SQL 相同的位,即这一行:

AND ((genres LIKE '%' || 'Western' || '%') OR (genres LIKE '%' || 'Drama' || '%')) 

注意:这也return没什么:

AND ((genres LIKE @genre1) OR (genres LIKE @genre2)) 

在sql语句中,您传递的参数不能用单引号引起来。
还让 SQLite 使用 || 运算符将 '%' 通配符连接到参数。 所以这样写你的代码:

WHERE IMDBRating >= @IMDBMinRating 
AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
AND ((genres LIKE '%' || @genre1 || '%') OR (genres LIKE '%' || @genre2 || '%')) 
AND (MPAARating = @mpaaRating1)

更新:
我看到您为流派添加了这样的参数值:

cmd.Parameters.AddWithValue("@genre1",string.Format("%{0}%", genresSelected[0].ToString()));

因此您在字符串中包含了通配符。
这意味着不需要在 sql 语句中再次连接它们。
因此,请按原样使用运算符 LIKE 和参数:

WHERE IMDBRating >= @IMDBMinRating 
AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
AND ((genres LIKE @genre1) OR (genres LIKE @genre2)) 
AND (MPAARating = @mpaaRating1)

我认为问题出在您的查询中。看起来您正在使用参数化查询,因此您应该使用:

var query = "SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors, screenwriters 
FROM MOVIES_SINGLETABLE 
WHERE IMDBRating >= @IMDBMinRating 
AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
AND (genres LIKE '%'+@genre1+'%' OR genres LIKE '%'+@genre2+'%') 
AND (MPAARating = @mpaaRating1) 
ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000";

...

cmd.Parameters.AddWithValue("@genre1","gener1");
cmd.Parameters.AddWithValue("@genre2","gener2");
cmd.Parameters.AddWithValue("@mpaaRating1","rating");

var query = "SELECT MovieTitle, MPAARating, IMDBRating, DurationInMinutes, YearReleased, genres, actors, directors, screenwriters 
FROM MOVIES_SINGLETABLE 
WHERE IMDBRating >= @IMDBMinRating 
AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
AND (genres LIKE @genre1 OR genres LIKE @genre2) 
AND (MPAARating = @mpaaRating1) 
ORDER BY IMDBRating DESC, YearReleased DESC LIMIT 1000";

...

cmd.Parameters.AddWithValue("@genre1","%gener1%");
cmd.Parameters.AddWithValue("@genre2","%gener2%");
cmd.Parameters.AddWithValue("@mpaaRating1","rating");

在大多数应用层 SQL 参数化中,您必须在原始字符串上连接通配符 ,然后 将它们绑定到准备好的 SQL 语句。

if (GenresFiltered())
{
    if (genresSelected.Count > 0)
    {
        cmd.Parameters.AddWithValue("@genre1", "%" + genresSelected[0].ToString() + "%");
        // cmd.Parameters.AddWithValue("@genre1", String.concat("%", genresSelected[0].ToString(), "%"));
    }
    if (genresSelected.Count > 1)
    {
        cmd.Parameters.AddWithValue("@genre2", "%" + genresSelected[1].ToString() "%");
        // cmd.Parameters.AddWithValue("@genre2", String.concat("%", genresSelected[1].ToString(), "%"));
    }
    . . .

此外,准备语句中的所有参数通常不包含在引号、通配符或其他符号中。

SELECT MovieTitle
       , MPAARating
       , IMDBRating
       , DurationInMinutes
       , YearReleased
       , genres
       , actors
       , directors
       , screenwriters 
FROM MOVIES_SINGLETABLE 
WHERE IMDBRating >= @IMDBMinRating 
  AND (YearReleased BETWEEN @EarliestYear AND @LatestYear) 
  AND (genres LIKE @genre1 OR genres LIKE @genre2) 
  AND (MPAARating = @mpaaRating1) 
ORDER BY IMDBRating DESC, YearReleased DESC 
LIMIT 1000

我终于明白了:

    return string.Format("AND instr(genres, '{0}') > 0 OR instr(genres, '{1}') > 0 ", 
               genresSelected[0].ToString(), genresSelected[1].ToString());