MySQL syntax mistake - java

When I try to execute:
SELECT matricula FROM lss_alumnos WHERE lss_alumnos.division = 'PREP';
I get this message:
SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.division = 'PREP'' at line 1
And I am also using MySQL Workbench, and there I got the right result.

Try removing the qualified name lss_alumnos before the column name, also, remove the leading ;.
SELECT `matricula` FROM `lss_alumnos` WHERE `division` = 'PREP'
(It's also a good practice to surround the names with the ` symbol).

I believe the comments are closer to the truth than the only other answer. To be sure, this query will work:
SELECT la.`matricula` FROM `lss_alumnos` la WHERE la.`division`='PREP'
Edit: I checked. None of these words are a MySQL reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html. It must be the semicolon

Related

Jooq count method getting wrapped in backticks

I recently updated a project's jooq version from 3.13.5 to 3.14.15. I've rerun jooq-codegen. I'm using MySQL 5.7.
When running one of my tests - it performs a DAOImpl.exists. That call generates the following exception:
org.jooq.exception.DataAccessException: SQL [select `count`(*) from `users` where `users`.`id` = ?]; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from `users` where `users`.`id` = 1094' at line 1
at org.jooq_3.14.15.MYSQL.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2903)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:757)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:389)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:453)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:422)
at org.jooq.impl.AbstractResultQuery.fetchLazyNonAutoClosing(AbstractResultQuery.java:436)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:613)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:565)
at org.jooq.impl.SelectImpl.fetchOne(SelectImpl.java:3034)
at org.jooq.impl.DAOImpl.existsById(DAOImpl.java:300)
at org.jooq.impl.DAOImpl.exists(DAOImpl.java:288)
The problem is that count has backticks. It should be select count(*) from `users` where `users`.`id` = ?
Has anyone run into this before and know how to fix it? TIA
You probably specified RenderQuotedNames.ALWAYS and ran into this problem? https://github.com/jOOQ/jOOQ/issues/9931
The setting ALWAYS is misleading. It really means literally always, though people probably read it as ALWAYSISH_I_E_ONLY_WHEN_IT_MAKES_SENSE. Probably a naming design error, but what you want is EXPLICIT_DEFAULT_QUOTED, see the documentation:
https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-name-style/
RenderQuotedNames
ALWAYS: This will quote all identifiers.
EXPLICIT_DEFAULT_QUOTED: This will quote all identifiers, which are not explicitly unquoted using DSL.unquotedName().
EXPLICIT_DEFAULT_UNQUOTED: This will not quote any identifiers, unless they are explicitly quoted using DSL.quotedName().
NEVER: This will not quote any identifiers.
COUNT in DSL.count()is an explicitly unquoted name, and ALWAYS will override that.

MariaDB JDBC chokes on column name containing curly brackets

I have a very simple SQL query I need to run
SELECT `id`, `{Document id}` FROM `test`.`test` LIMIT 10;
where {Document id} is a column name. Whenever I run it through MariaDB JDBC, it fails with error unknown escape sequence. From my understanding {CALL ...} is used to call stored procedure with a JDBC CallableStatement.
How do I escape it? I want JDBC to treat it as literal string without special meaning. \ didn't work for me.
As mentioned in deleted answer by #a_horse_with_no_name, there is setEscapeProcessing. But it's not supported by a lot of connectors (example MariaDB).
I have confirmed this issue using mariadb-java-client-2.2.5. It is not an issue with mysql-connector-java-5.1.44 so you might want to report this issue to MariaDB.
Following the suggestion by #JoopEggen in the comments above, I got it to work with MariaDB JDBC by adding sessionVariables=sql_mode=ANSI_QUOTES to the connection string and using
ResultSet rs = st.executeQuery("SELECT \"{Document id}\" FROM test.test");

JDBC throws MySQL syntax Exception, despite valid query

Hello helpful folks,
I've a small part in an application which creates a new db schema based on a mysqldump.
The relevant part looks like this:
log.info("= setup basic database");
// Execute each command in the dump
for (String query : dump.split(";")) {
statement.execute(query);
}
This works fine but some statements just break with something like
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ot_subtotal.php' at line 1
The query in this case is
INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php;ot_discount.php;ot_coupon.php;ot_shipping.php;ot_cod_fee.php;ot_gv.php;ot_subtotal_no_tax.php;ot_tax.php;ot_total_netto.php;ot_total.php',6,0,NULL,'2016-06-17 15:27:24',NULL,NULL);
If I replace the string "'ot_subtotal.php;ot_discount.php;ot_coupon.php;ot_shipping.php;ot_cod_fee.php;ot_gv.php;ot_subtotal_no_tax.php;ot_tax.php;ot_total_netto.php;ot_total.php'" with something like "hello world" it works and breaks at a another query later on.
The thing is, I'm able to import the dump via the terminal command "mysql ... < dump.sql" without a problem.
Any ideas?
This is happening because of
for (String query : dump.split(";")) {
statement.execute(query);
}
You are splitting at ; which result in incomplete queries.
For example:
INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php;ot_discount.php;ot_coupon.php;ot_shipping.php;ot_cod_fee.php;ot_gv.php;ot_subtotal_no_tax.php;ot_tax.php;ot_total_netto.php;ot_total.php',6,0,NULL,'2016-06-17 15:27:24',NULL,NULL);
Above query after split at ; will be
INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php
Which is incorrect.
Instead of
dump.split(";")
Use
dump.split(";\n")
If you created the dump file in normal way.

The following query causes a MySQLSyntaxErrorException - what is wrong with my query?

The following exception is thrown when ever I execute the following query
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '-name, cpy-address, cpy-contact) VALUES('nauman','ahmad18',12)' at line 1
Query which is causing exception
String query="insert into company(cpy-name, cpy-address, cpy-contact)VALUES(?,?,?)";
Connection con=DataAccessLayer.getConnection();
PreparedStatement stat=con.prepareStatement(query);
stat.setString(1, cname);
stat.setString(2, caddress);
stat.setInt(3,x );
int rowsAffected = stat.executeUpdate();
You cannot use - in SQL queries. Escape the column names using ` back quote or back tick characters.
insert into company(`cpy-name`, `cpy-address`, `cpy-contact`)VALUES(?,?,?)
On another note, if the database is in your control, change to column names to use '_' rather '-'. Having illegal characters and quoting them is not a good practice.
Hope this helps!
As #uday said you should change column name or use
" ` "
You can't use dash - symbol in SQL query in case of column name so that rename your column name or use Escape ` both side of column name and table name.

SELECT * FROM table throws error in JDBC

I am trying to make a select from a table, which works fine with every other table in my database, but when I try the following I recieve an error:
db.makeQuery("SELECT * FROM References");
Which calls:
public ResultSet makeQuery(String query) throws Exception
{
preparedStatement = connect.prepareStatement(query);
resultSet = preparedStatement.executeQuery(query);
return resultSet;
}
It then throws the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'References' at line 1
I seems very strange to me, since this statement works:
db.makeQuery("select * from Products");
References is a keyword in SQL, so you better avoid it for table names. (See for instance this documentation.)
As suggested by Nishant, you can use reserved words in queries if you escape them with backticks.
Related question:
Using MySQL keywords in a query?
use
db.makeQuery("SELECT * FROM `References`");
if you can, better, avoid having names that are MySQL keywords. As suggested by aioobe
You might be misspelling the name of your table. MySQL gives this error when it can't find that table you're referring to.
Use SHOW TABLES; to see the names of the tables in your database, and double-check the name.

Categories

Resources