java HSQLDB 意外令牌:?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42222261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HSQLDB unexpected token:?
提问by Sebu
I have a JAVAFX project using HSQLDB. When trying to set the SOURCE of a table I get an exception I think I understand, but since I cant fix it I guess I dont understand it. My SQL is:
我有一个使用 HSQLDB 的 JAVAFX 项目。尝试设置表的 SOURCE 时,出现异常,我想我明白了,但由于我无法修复它,我想我不明白。我的 SQL 是:
DROP TABLE temp IF EXISTS;
CREATE TEXT TABLE temp(text_data LONGVARCHAR(10000));
SET TABLE temp SOURCE ?;
INSERT INTO log(typ, json) SELECT SUBSTRING(text_data, 3, LOCATE('"', text_data, 3)-3),text_data FROM temp WHERE text_data <> '';
DROP TABLE temp IF EXISTS;
Mutliple Statements somehow do not work for me here, and this should not be a problem for now. Im splitting the sql above into an ArrayList of Strings, with each line being one element. So I got this Java Code:
在这里,多重语句不知何故对我不起作用,现在这应该不是问题。我将上面的 sql 拆分为字符串的 ArrayList,每一行都是一个元素。所以我得到了这个 Java 代码:
s = c.createStatement();
for (String sql : sqls) {
System.out.println("sql: " + sql);
if (sql.contains("?")) {
System.out.println("in ? part");
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, path.toUri().toString() + ";encoding=UTF-8;ignore_first=false;fs=\n\r");
System.out.println("ps prepared" + ps.toString());
ps.execute();
} else {
s.execute(sql);
}
}
And my application is failing at line PreparedStatement ps = c.prepareStatement(sql);
with the following exception:
我的应用程序失败,PreparedStatement ps = c.prepareStatement(sql);
但出现以下异常:
java.sql.SQLSyntaxErrorException: unexpected token: ? in statement [SET TABLE temp SOURCE ?;]
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at myfile in the line I pointed out above
at anotherofmyfiles
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hsqldb.HsqlException: unexpected token: ?
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
at org.hsqldb.ParserBase.checkIsValue(Unknown Source)
at org.hsqldb.ParserBase.readQuotedString(Unknown Source)
at org.hsqldb.ParserCommand.compileTableSource(Unknown Source)
at org.hsqldb.ParserCommand.compileSetTable(Unknown Source)
at org.hsqldb.ParserCommand.compileSet(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 7 more
with this output before:
之前有这个输出:
sql: DROP TABLE temp IF EXISTS;
sql: CREATE TEXT TABLE temp(text_data LONGVARCHAR(10000));
sql: SET TABLE temp SOURCE ?;
in ? part
I am aware that ps.setString(1, path.toUri().toString() + ";encoding=UTF-8;ignore_first=false;fs=\\n\\r");
may not be completely correct in semantics, but syntaxwise it should work and since the error is before that it should not be a cause to this error. When I am running the application without that line the same error occurs.
我知道这ps.setString(1, path.toUri().toString() + ";encoding=UTF-8;ignore_first=false;fs=\\n\\r");
在语义上可能不完全正确,但在语法上它应该可以工作,并且由于错误在此之前,它不应该是导致此错误的原因。当我在没有该行的情况下运行应用程序时,会发生同样的错误。
So my question is: What is wrong with SET TABLE temp SOURCE ?;
? Why cant I use this as a PreparedStatement in Java? As I understand from the documentationthe syntax is SET TABLE <tablename> SOURCE <quoted_filename_and_options>
where <quoted_filename_and_options>
is a string. Cant I prepare that in Java?
所以我的问题是:有什么问题SET TABLE temp SOURCE ?;
?为什么我不能将它用作 Java 中的 PreparedStatement?当我从理解文档的语法是SET TABLE <tablename> SOURCE <quoted_filename_and_options>
这里<quoted_filename_and_options>
是一个字符串。我不能用 Java 准备它吗?
回答by john16384
PreparedStatements are sent to underlying SQL engine for compilation. The location where you are allowed to use parameters depends on the driver and engine. Usually they're only supported in very specific places as otherwise a statement cannot really be compiled.
PreparedStatements 被发送到底层 SQL 引擎进行编译。允许使用参数的位置取决于驱动程序和引擎。通常它们只在非常特定的地方受支持,否则语句无法真正编译。
Consider a PreparedStatement that only contains "?", and you supply a parameter:
考虑一个只包含“?”的 PreparedStatement,并提供一个参数:
ps.setString(1, "SELECT * FROM myTable");
This can't be compiled, so it gets rejected.
这无法编译,因此被拒绝。
Therefore most SQL database only support parameters in INSERT/UPDATE/SELECTS in positions where a simple value would normally appear. They can't be used for field names, table names, etc.
因此,大多数 SQL 数据库仅支持 INSERT/UPDATE/SELECTS 中通常出现简单值的位置的参数。它们不能用于字段名、表名等。