在 Python ODBC 中使用 Microsoft Access SQL 运算符

发布于 2024-09-27 20:50:38 字数 1533 浏览 0 评论 0原文

简短版本:当我尝试通过 ODBC 使用 Access 的 DatePart 函数时,无法解析。

更长的版本:

我有一个 Microsoft Access 查询,它返回带有时间戳和分数的行。我想按排序,然后按分数排序 - 实际上是当天的高分表。

为了获得更好的功能,我使用 DatePart 函数从时间戳中提取每个年、月和日,并 ORDER BY 它们后跟 Score。

在 Microsoft Access 中,查询工作得非常好。

但是,当我使用 pyodbc 访问相同的查询时,ODBC 驱动程序被 DatePart 函数所困扰,并认为它是缺少参数的名称。

让我惊讶的是,即使我通过创建一个新的 HighScore 查询来隐藏 DatePart 函数,然后 SELECT * FROM HighScore ,它仍然抱怨找不到参数。显然,查询的 SQL 在此过程中相当晚才得到解决。

我的问题是:

  • 如何解析 SQL 中的 DatePart 函数以允许 Access 运行它,或者
  • 通过 ODBC 按时间戳的日期部分排序的正确方法是什么?

添加了附加信息:

看起来有些过头了,但这里有一些代码:

import pyodbc
access_db_path = r"<ellided>"
connection_string = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+access_db_path
connection = pyodbc.connect(connection_string, autocommit = True)

print "First query"
connection.cursor().execute('SELECT ScoreTime FROM SplitExtendedP1')
print "Worked"

print "Second query"
print connection.cursor().execute('SELECT DatePart("yyyy",ScoreTime) FROM SplitExtendedP1')
print "Doesn't get here."

以下是结果:

First query
Worked
Second query
Traceback (most recent call last):
  File "<ellided>.py", line 16, in <module>
    print connection.cursor().execute('SELECT DatePart("yyyy", ScoreTime) FROM SplitExtendedP1')
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. (-3010) (SQLExecDirectW)')

Short version: When I try to use Access's DatePart function via ODBC, it cannot be resolved.

Longer version:

I have a Microsoft Access query which returns rows with a timestamp and a score. I want to sort it by day and then by score - effectively a high-score table for the day.

For want of a better function, I used the DatePart function to extract each of the Year, Month and Day from the timestamp, and ORDER BY them followed by Score.

In Microsoft Access, the query works beautifully.

However, when I use pyodbc to access the same query, the ODBC driver is stumped by the DatePart function, and thinks it is the name of a missing parameter.

What astonished me was that even if I hid the DatePart function, by creating a new HighScore query, and then SELECT * FROM HighScore, it still complained that it couldn't find the parameter. Apparently, the query's SQL is being resolved fairly late in the process.

My question is either:

  • How do I resolve the DatePart function in the SQL to allow Access to run it, or
  • What is a right way to sort by the day part of a timestamp over ODBC?

Added additional info:

Seems like overkill, but here's some code:

import pyodbc
access_db_path = r"<ellided>"
connection_string = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+access_db_path
connection = pyodbc.connect(connection_string, autocommit = True)

print "First query"
connection.cursor().execute('SELECT ScoreTime FROM SplitExtendedP1')
print "Worked"

print "Second query"
print connection.cursor().execute('SELECT DatePart("yyyy",ScoreTime) FROM SplitExtendedP1')
print "Doesn't get here."

Here are the results:

First query
Worked
Second query
Traceback (most recent call last):
  File "<ellided>.py", line 16, in <module>
    print connection.cursor().execute('SELECT DatePart("yyyy", ScoreTime) FROM SplitExtendedP1')
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. (-3010) (SQLExecDirectW)')

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

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

发布评论

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

评论(2

花心好男孩 2024-10-04 20:50:38

您确定使用引号 "yyyy" 而不是撇号 'yyyy' 在该 SQL 方言中有效吗?

Are you sure that using quotes "yyyy" instead of apostrophes 'yyyy' is valid in that dialect of SQL?

始于初秋 2024-10-04 20:50:38

日期时间以浮点数形式存储在 access 中。小数点左边的数字是日期,小数点右边的小数部分是时间(以一天的小数形式表示;即 0.5 = 中午)。

如果您想按日期排序,只需使用 Int() 函数即可返回日期时间字段的整数部分。

Datetimes are stored in access as floating point numbers. The number to the left of the decimal point is the date, the fractional part to the right of the decimal point is the time (expressed in terms of a fraction of a day; ie, .5 = Noon).

If you want to sort by the day, you can simply use the Int() function to return the integer portion of the datetime field.

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