C、unixODBC、oracle 和日期查询
我有一个查询,例如
select x from tableName where startDate <= now;
当我查询没有日期字段的数据库时,一切都按预期进行。一旦我开始在 Oracle 数据库中使用日期或时间戳列,我的查询就不会返回任何内容或返回错误。
我做什么:
snprintf(sql, sizeof(sql), "SELECT roomNo, userPass, adminPass, adminFlags, userFlags, bookId, is_locked, running_on_server FROM booking WHERE roomNo = '?' AND startTime <= { ts '?' } AND endTime >= { ts '?' } for update;");
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
?将被值替换,使用以下命令:
SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->argv[i]), 0, gps->argv[i], 0, NULL);
当我执行查询时,我收到一条错误,指出 TS 是无效标识符 [这是另一个委员会的建议,取自 msdn - 这可能会导致此错误]
但即使我将其删除并将一个字符串发送到数据库,我会得到一个空结果。我还尝试将参数绑定为 SQL_TIMESTAMP 和 SQL_DATE,但这也没有帮助。
希望有人可以帮助我。
提前致谢。 克里斯
I've got a query like
select x from tableName where startDate <= now;
When i query the database without the date fields, everything works as expected. As soon as i start using date or timestamp columns in the oracle Database, my queries return nothing or an error.
What I do:
snprintf(sql, sizeof(sql), "SELECT roomNo, userPass, adminPass, adminFlags, userFlags, bookId, is_locked, running_on_server FROM booking WHERE roomNo = '?' AND startTime <= { ts '?' } AND endTime >= { ts '?' } for update;");
stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
the ? will be replaced by values, with following command:
SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->argv[i]), 0, gps->argv[i], 0, NULL);
when I execute the query I get an error that TS is an invalid identifier [this was a recommendation by another board, taken from msdn - which may cause this error]
but even if I remove it and send just a string to the database, I'll get an empty result back. I also tried to bind the parameters as SQL_TIMESTAMP and SQL_DATE, but this didn't help either.
Hopefully somebody can help me.
thanks in advance.
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否向 Oracle 查询发送 DATE 数据类型或以字符串表示的日期?
从 Oracle 方面来看,如果您在字符串变量中发送日期,则需要使用 Oracle“TO_DATE”函数 (http://www.techonthenet.com/oracle/functions/to_date.php) 然后将字符串转换回 SQL 语句中使用的日期(假设 startDate 或startTime/endTime 是数据库中的 DATE 列)。
您的第一个 SQL 示例应该是:
如果变量“now”是包含“05-OCT-2011 16:15:23”(包括时间部分)的字符串,则 to_date 将是:
如果将字符串与日期进行比较,并且不指定该日期的格式 Oracle 将使用其默认 NLS 参数并尝试应用该格式。因此,使用 TO_DATE 指定日期格式始终是谨慎的做法。
希望它有帮助...
Are you sending a DATE data type to the Oracle query or a date represented in a string?
From the Oracle side of things, if you send a date in a string variable you'll need to use the oracle "TO_DATE" function (http://www.techonthenet.com/oracle/functions/to_date.php) to then convert thew string back to a date for use in your SQL statement (assuming startDate or startTime/endTime are DATE columns in the database).
Your fist SQL example should be:
If the variable "now" was a string containing '05-OCT-2011 16:15:23' (including a time portion) then the to_date would be:
If you compare a string with a date and don't specify the format of that date Oracle will use its default NLS parameters and try to apply that format. Therefore it is always prudent to specify the date format using TO_DATE.
Hope it helps...