繁体   English   中英

Python:sqlite3参数化查询没有结果

[英]Python: sqlite3 parametrized query gets no results

我尝试使用参数化查询从表中选择条目。 他们不返回任何结果。 代码如下:

var = str.capitalize(var)
selected = db.execute('select a, b, c from table1 where a=(?)', [var])

var始终是三个小写的字符串(例如,“ xxx”),数据库中的“ a”列的类型为TEXT,并且包含三个大写的字符串(例如,“ XXX”)。

我也尝试了可怕的:

selected = db.execute('select a, b, c from table1 where a="%s"' % str.capitalize(var)])

因为我认为执行方法省略引号是一个问题,但是它也不起作用。 唯一让我有任何结果的是:

selected = db.execute('select a, b, c from table1 where a="XXX"')

我在Windows 10上使用Python 3.6.0, 这里有人建议说这可能是个问题,但是他们的解决方案也对我不起作用。

capitalize仅将第一个字母转换为大写。 要将所有内容转换为大写,请使用upper()

参数必须作为元组/字典传递

采用

selected = db.execute('select a, b, c from table1 where a=?', (var.upper(),))

要么

selected = db.execute('select a, b, c from table1 where a=:var', {"var":var.upper()})

在这里阅读更多

您必须使用upper将其转换为全部大写,请尝试以下操作:

var = var.upper()
selected  =  db.execute('select a, b, c from table1 where a=?', (var,))

这个也可以工作:

var = var.upper()
selected = db.execute('select a, b, c from table1 where a="%s"'%var]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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