str_to_date 在 HSQL 中无法运行。

huangapple go评论84阅读模式
英文:

str_to_date mysql does not run in hsql

问题

我的代码在给定时间在MySQL数据库中执行以下更新:

"UPDATE client_registration " +
"SET registration_date = NOW() " +
"WHERE cycle <= str_to_date('" + now + "','%d/%m/%Y %H:%i');"

然而,我有一个单元测试尝试在HSQL数据库上执行此更新,但我收到以下错误消息:用户缺乏权限或对象未找到:STR_TO_DATE

有没有办法在MySQL数据库和HSQL数据库中执行条件WHERE cycle_start <= str_to_date('" + now + "','%d/%m/%Y %H:%i')

英文:

My code is performing the following update at a given time in my mysql database:

&quot; UPDATE client_registration &quot;  +
&quot; SET registration_date = NOW() &quot;  +
&quot; WHERE cycle &lt;= str_to_date(\&quot;&quot; + now + &quot;\&quot;,&#39;%d/%m/%Y %H:%i&#39;) &quot;; 

However I have a unit test that tries to perform this update on the HSQL database and I receive the following error message: user lacks privilege or object not found: STR_TO_DATE.

Some way to execute the condition WHERE cycle_start &lt;= str_to_date(\&quot;&quot; + now + &quot;\&quot;,&#39;%d/%m/%Y %H:%i&#39;) for the mysql database and the hsql database?

答案1

得分: 0

你需要重新编写针对 HSQL 的查询:

"UPDATE client_registration " +
"SET registration_date = NOW() " +
"WHERE cycle <= current_timestamp";


https://stackoverflow.com/questions/909702/how-to-do-select-current-timestamp-in-hsqldb
英文:

You have to re-write your query for HSQL:

&quot; UPDATE client_registration &quot;  +
&quot; SET registration_date = NOW() &quot;  +
&quot; WHERE cycle &lt;= current_timestamp&quot;; 

https://stackoverflow.com/questions/909702/how-to-do-select-current-timestamp-in-hsqldb

答案2

得分: 0

如果您想在MySQL和HSQLDB上运行完全相同的查询,需要在HSQLDB上创建STR_TO_DATE函数。您还需要在查询中使用单引号字符:str_to_date('"+now+"','%d/%m/%Y %H:%i')(此引号遵循SQL标准)。

如果您使用SQL标准格式'YYYY-MM-DD hh:mm:ss'(例如'2020-07-21 14:30:00')作为您的'now'变量和与MySQL一起使用的格式字符串,会更容易。在这种情况下,HSQLDB函数可以简单地创建为:

CREATE FUNCTION STR_TO_DATE(STR VARCHAR(30), FORMAT VARCHAR(40) ) 
 RETURNS TIMESTAMP
 RETURN CAST(STR AS TIMESTAMP);

连接到数据库时执行一次CREATE FUNCTION语句,然后您可以在所有查询和更新语句中使用它。

英文:

If you want to run the exact same query on both MySQL and HSQLDB, you need to create the STR_TO_DATE function on HSQLDB. You also need to use the single-quote character in your query: str_to_date(&#39;&quot; + now + &quot;&#39;,&#39;%d/%m/%Y %H:%i&#39;) (this quoting follows the SQL Standard).

It is easier if you use the SQL Standard format 'YYYY-MM-DD hh:mm:ss' (e.g '2020-07-21 14:30:00') for your 'now' variable and the format string you use with MySQL. In this case the HSQLDB function is simply created as:

CREATE FUNCTION STR_TO_DATE(STR VARCHAR(30), FORMAT VARCHAR(40) ) 
 RETURNS TIMESTAMP
 RETURN CAST(STR AS TIMESTAMP);

Execute the CREATE FUNCTION statement once when you connect to the database and you can use it in all your queries and upade statements.

huangapple
  • 本文由 发表于 2020年7月22日 04:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63022512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定