在 Python ODBC 中使用 Microsoft Access SQL 运算符
简短版本:当我尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定使用引号
"yyyy"
而不是撇号'yyyy'
在该 SQL 方言中有效吗?Are you sure that using quotes
"yyyy"
instead of apostrophes'yyyy'
is valid in that dialect of SQL?日期时间以浮点数形式存储在 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.