'投'使用bigint和varchar在mysql中出现问题

时间:2016-05-31 17:34:11

标签: mysql casting

如下所示,使用' cast'与int& char工作正常, 但与bigint和amp;相同的命令varchar分别没有。

这是mysql中的已知问题吗? 我在使用jooq时遇到了这个问题 - 详情如下。

如果有这方面的解决方法,在mysql或jooq中,这将是很棒的。 谢谢!

$ mysql --version
mysql  Ver 15.1 Distrib 5.5.39-MariaDB, for debian-linux-gnu (x86_64) using readline 5.1

SQL命令 这两个工作正常

MariaDB [puresw]> select cast(100 as int);
+------------------+
| cast(100 as int) |
+------------------+
|              100 |
+------------------+
1 row in set (0.01 sec)

MariaDB [puresw]> select cast('abc' as char(32));
+-------------------------+
| cast('abc' as char(32)) |
+-------------------------+
| abc                     |
+-------------------------+
1 row in set (0.00 sec)

这两个人没有

MariaDB [puresw]> select cast(100 as bigint);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'bigint)' at line 1

MariaDB [puresw]> select cast('abc' as varchar(32));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'varchar(32))' at line 1

由jooq生成的SQL查询引起的问题

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'bigint), cast(0 as int), cast('ABC' as varchar), cast('Purity' as varchar), cast' at line 1
Query is:
sql : 'insert into software_version (task_id, controller, group_id, name, version, status) values (cast(? as bigint), cast(? as int), cast(? as varchar), cast(? as varchar), cast(? as varchar), cast(? as varchar))', parameters : [100,0,'ABC','Purity','4.5.2','Available']
    at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:138) ~[mariadb-java-client-1.1.9.jar:?]
    at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106) ~[mariadb-java-client-1.1.9.jar:?]
    at org.mariadb.jdbc.MySQLStatement.executeQueryEpilog(MySQLStatement.java:268) ~[mariadb-java-client-1.1.9.jar:?]
    at org.mariadb.jdbc.MySQLStatement.execute(MySQLStatement.java:296) ~[mariadb-java-client-1.1.9.jar:?]
    at org.mariadb.jdbc.MySQLStatement.executeUpdate(MySQLStatement.java:364) ~[mariadb-java-client-1.1.9.jar:?]
    at org.mariadb.jdbc.MySQLPreparedStatement.executeUpdate(MySQLPreparedStatement.java:148) ~[mariadb-java-client-1.1.9.jar:?]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) ~[commons-dbcp-1.4.jar:1.4]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) ~[commons-dbcp-1.4.jar:1.4]
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:916) ~[spring-jdbc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:909) ~[spring-jdbc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644) ~[spring-jdbc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    ... 43 more

1 个答案:

答案 0 :(得分:0)

这是用户错误。我正在使用为H2数据库的SQL方言配置的jooq上下文。创建了另一个与mysql一起使用的jooq上下文,问题就消失了。这就是现在dao xml文件中的jooq部分:

<bean id="jooqSettings" class="org.jooq.conf.Settings">
    <property name="renderNameStyle" value="AS_IS"/>
</bean>

<bean id="jooqConfig" class="org.jooq.impl.DefaultConfiguration">
    <property name="SQLDialect" value="H2"/>
    <property name="settings" ref="jooqSettings"/>
</bean>

<bean id="jooqContext" class="org.jooq.impl.DefaultDSLContext">
    <constructor-arg ref="jooqConfig"/>
</bean>

<bean id="jooqConfigMysql" class="org.jooq.impl.DefaultConfiguration">
    <property name="SQLDialect" value="MYSQL"/>
    <property name="settings" ref="jooqSettings"/>
</bean>

<bean id="jooqContextMysql" class="org.jooq.impl.DefaultDSLContext">
    <constructor-arg ref="jooqConfigMysql"/>
</bean>