SQLite Python AND 语句不起作用

发布于 2024-10-19 10:48:57 字数 573 浏览 8 评论 0原文

我正在尝试从 SQLite 数据库中提取一周中给定日期和当天特定时间的行列表。 当我打开我的 sqlite 浏览器并输入 SQL 时:

SELECT * FROM points
WHERE strftime('%H', checkintime) == '12'
  AND strftime('%w', checkintime) == '1'

它起作用了,我收到了我应该收到的所有行。 然而,当我尝试在 python 中执行此操作时:

_points.execute("""SELECT * FROM points WHERE strftime('%H', checkintime) == ? AND strftime('%w', checkintime) == ?""", (time, day))

其中时间为 12,日期为 1,我返回的结果为零。 更奇怪的是,当我将 execute 语句更改为使用硬编码值时,它起作用了。

我尝试将我的变量类型转换为字符串和整数,但仍然不行。 :(

有什么建议吗?

I'm attempting to pull a list of rows from an SQLite database that are on a given day of the week and a certain hour of that day.
When I open my sqlite browser and type the SQL:

SELECT * FROM points
WHERE strftime('%H', checkintime) == '12'
  AND strftime('%w', checkintime) == '1'

It works and I receive all of the rows I should be receiving.
However when I attempt to do this in python:

_points.execute("""SELECT * FROM points WHERE strftime('%H', checkintime) == ? AND strftime('%w', checkintime) == ?""", (time, day))

Where time is 12 and day is 1 I get zero results returned.
What's even more strange is when I change the execute statement to use hardcoded values, it works.

I've attempted to typecast my vars to both strings and ints but its still a no go. :(

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-10-26 10:48:57

我认为你必须将数字参数转换为字符串,否则它们不会像你在 sqlite 语句中那样被引用。变量替换为 ?不引用数字。

您实际上是在比较字符串,因此必须将参数转换为字符串。

I reckon you have to convert your number parameters to strings, otherwise they won't get quoted as you do in your statement in sqlite. The variable substitution with ? does not quote numbers.

You are actually comparing strings, so you must convert the parameters to strings.

握住你手 2024-10-26 10:48:57

时间和日期值确实需要是字符串。另外,我想知道你的 '%' 是否需要转义(用 %% )——有可能在 pysqlite 内部 ? 占位符被转换为 % 样式的占位符,然后你的 %H 和 %w 被误解。下面的方法有效吗?

_points.execute("""SELECT * FROM points WHERE strftime('%%H', checkintime) == ? AND strftime('%%w', checkintime) == ?""", (str(time), str(day)))

The time and day values do need to be strings. Also, I wonder if your '%' needs escaping (with %%) -- It's possible that inside pysqlite the ? placeholders get converted to %-style ones and then your %H and %w get misinterpreted. Does the following work?

_points.execute("""SELECT * FROM points WHERE strftime('%%H', checkintime) == ? AND strftime('%%w', checkintime) == ?""", (str(time), str(day)))
征棹 2024-10-26 10:48:57

在执行调用之外处理字符串格式怎么样?例如,

sql_statement = """SELECT * FROM points WHERE strftime('%s', checkintime) == %s AND strftime('%s', checkintime) == %s""" % ('%H', time, '%w', day)

_points.execute(sql_statement)

这应该避免执行语句无法完成的字符串映射。

祝你好运!

How about you handle the string formatting outside of the execute call? For example,

sql_statement = """SELECT * FROM points WHERE strftime('%s', checkintime) == %s AND strftime('%s', checkintime) == %s""" % ('%H', time, '%w', day)

_points.execute(sql_statement)

This should avoid the string mapping that the execute statement is unable to accomplish.

Best of luck!

夜雨飘雪 2024-10-26 10:48:57

尝试这样:
_points.execute("""SELECT * FROM 点 WHERE strftime('%H', checkintime) == ? AND strftime('%w', checkintime) == ?"""%(time , day))

注意 %(time, day)

我不确定 %H 和 %w ,不知道时间和日期格式是什么

try like this:
_points.execute("""SELECT * FROM points WHERE strftime('%H', checkintime) == ? AND strftime('%w', checkintime) == ?"""%(time, day))

pay attention to %(time, day)

I'm not sure about the %H and %w , don't know what time and day format is

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文