<p>可以对SQLite实例而不是Python运行SQL。这样,就更容易发现SQL语句的问题——少了一层</p>
<p>我发现您的查询的语法可能存在问题。当使用<code>{country}</code>时,Python将<code>Canada</code>、<code>Poland</code>作为字符串。但是SQLite需要的值应该是<code>'Canada'</code>,<code>'Poland'</code></p>
<p>除此之外,<code>IN</code>运算符检查提供的列表中是否存在字段-这就是为什么需要<code>(</code>,<code>)</code>用一个元素构造列表。这里更好的运算符是<code>=</code></p>
<p>如果我碰了操作员。。。请阅读<code>BETWEEN</code>{a1}</p>
<p>下一个。我不确定<code>from_date</code>/<code>to_date</code>列中存储了什么。请阅读<a href="https://sqlite.org/lang_datefunc.html" rel="nofollow noreferrer">SQLite docs</a>以确定它是否正确使用</p>
<p>因此,对于提议的变更,我希望看到:</p>
<pre><code>sql = f"""SELECT *
FROM table
WHERE country_col = '{country}'
AND name_col = '{name}'
AND date(date_col) BETWEEN '{from_date}' AND '{to_date}'"""
</code></pre>
<h2>更新</h2>
<p>传递数组。让我们从基本SQL开始:</p>
<p>从国家(波兰、加拿大、德国)所在的表格中选择*)</p>
<p>要阅读更多信息,请查看<a href="https://www.sqlitetutorial.net/sqlite-in/" rel="nofollow noreferrer">this</a>站点。请记住,字符串应该被<code>'</code>包围。有时它也可以是<code>"</code>,但对于PostgreSQL来说<code>"col"</code>-表示列名<code>col</code>,而<code>'col'</code>表示字符串<code>col</code>。所以你的<code>IN</code>操作符是对的</p>
<p>现在从Python开始,您需要准备带有国家名称的字符串,每个国家名称由<code>'</code>包围:</p>
<pre><code>countries = ['Poland', 'Germany', 'France']
param = ", ".join([f"'{x}'" for x in countries])
</code></pre>
<p><code>join</code>连接传递列表中的字符串,内部<a href="https://diveinto.org/python3/comprehensions.html" rel="nofollow noreferrer">comprehension</a>将<code>'</code>添加到列表的每个项</p>
<p>一起:</p>
<pre><code>countries = ['Poland', 'Germany', 'France']
param = ", ".join([f"'{x}'" for x in countries])
sql = f"""SELECT *
FROM table
WHERE country_col in ({param})'
AND name_col = '{name}'
AND date(date_col) BETWEEN '{from_date}' AND '{to_date}'"""
</code></pre>