开发者

How do I select the last 5 minutes of data from an ms access db using pyodbc?

I have the following query (python pyodbc on windows 7 accessing an access db .mdb):

SQL = 'SELECT acq_spill_3_1_sec.time_stamp, acq_spill_3_1_se开发者_如何学编程c.float_value 
    FROM acq_spill_3_1_sec WHERE acq_spill_3_1_sec.time_stamp > DateADD(n,-5,Now())'

The DateADD doesn't seem to work. I get the following error:

pyodbc.Error: ('07002', '[070021] [Microsoft][ODBC Microsoft Access Driver] Too few
parameters. Expected 1.

I've tried single, double and no quotes around the n, but that makes no difference.


Two problems: 1) Put double quotes around the n parameter. 2) Put square brackets around the time_stamp. (Access interprets your underscore in the time_stamp field as a special character under certain circumstances. The query parser does its best to interpret special characters, but sometimes it gets it wrong. The square brackets help the parser disambiguate the field name so it does the parsing correctly.)

Just my opinion, I would use DateDiff instead, since it makes more sense when you read it:

SQL = 'SELECT time_stamp, float_value FROM acq_spill_3_1_sec WHERE DateDiff("n",[time_stamp],Now()) <= 5'

I find it's a lot easier to build the queries yourself inside Access and try them out there first to make sure they work before writing them in code. Good luck!


According to the examples on the DATADD MSDN page (http://msdn.microsoft.com/en-us/library/ms186819.aspx), try using

DATEADD(minute, -5, NOW())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜