CREATE LINKED TABLE 不适用于 PreparedStatement
Posted
技术标签:
【中文标题】CREATE LINKED TABLE 不适用于 PreparedStatement【英文标题】:CREATE LINKED TABLE not working with PreparedStatement 【发布时间】:2019-10-31 14:17:09 【问题描述】:我正在尝试运行以下 PreparedStatement:
final PreparedStatement ps = conn.prepareStatement("CREATE LINKED TABLE IF NOT EXISTS " + linkedName + "(?, ?, ?, ?, 'ROADS', ?)");
但是当调试器运行在上一行时,我得到了错误:
SQL 语句“CREATE LINKED TABLE IF NOT EXISTS”中的语法错误 ROAD_TABLE_LINKED(?,[*] ?, ?, ?, 'ROADS', ?)"; 预期的“字符串”; SQL 语句:如果不存在则创建链接表 ROAD_TABLE_LINKED(?, ?, ?, ?, 'ROADS', ?) [42001-199]
是否不能将 PreparedStatement 与 CREATE LINKED TABLE 一起使用?
编辑:如果我使用普通语句并插入参数,它就可以正常工作。
【问题讨论】:
【参考方案1】:很遗憾,您不能在 H2 的 DDL 命令中使用参数 (?
)。您需要用字符串文字替换它们。
Statement s = connection.createStatement();
s.execute("CREATE LINKED TABLE IF NOT EXISTS \""
+ linkedName.replaceAll("\"", "\"\"")
+ "\"('', '"
+ url.replaceAll("'", "''")
+ "', '"
+ user.replaceAll("'", "''")
+ "', '"
+ password.replaceAll("'", "''")
+ "', '"
+ schema.replaceAll("'", "''")
+ "', '"
+ table.replaceAll("'", "''")
+ "')");
【讨论】:
以上是关于CREATE LINKED TABLE 不适用于 PreparedStatement的主要内容,如果未能解决你的问题,请参考以下文章
为啥宽度:100% 不适用于 div display: table-cell?
VS Code Prettier - Code Formatter Extension 不适用于使用 Typescript 模板的 Create-React-App