搜索/插入查询jdbc中的反斜杠(\)和引号(') [英] Backslash (\) and Quote (') in search/insert query jdbc

查看:800
本文介绍了搜索/插入查询jdbc中的反斜杠(\)和引号(')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有cratedb的JDBC(几乎使用PSQL协议).碰巧当我尝试执行这样的查询

I am using JDBC with cratedb(which almost uses PSQL protocols). It happens that when I try to execute a query like this

String query = "select * from test where col1='dhruv\'";

它给了我解析器异常.

It gives me Parser exception.

Caused by: io.crate.shade.org.postgresql.util.PSQLException: Unterminated string literal started at position 39 in SQL select * from test where col1='dhruv\'. Expected  char
    at io.crate.shade.org.postgresql.core.Parser.checkParsePosition(Parser.java:1310)
    at io.crate.shade.org.postgresql.core.Parser.parseSql(Parser.java:1217)

然后在调试时,我在jdbc的Parser.java中发现了这段代码

Then on debugging I found that this code in Parser.java of jdbc

 else if (c == '\'') {
                    i0 = i;
                    i = parseSingleQuotes(p_sql, i, stdStrings);
                    checkParsePosition(i, len, i0, p_sql, "Unterminated string literal started at position {0} in SQL {1}. Expected ' char");
                    newsql.append(p_sql, i0, i - i0 + 1);
                }

正在引起问题.根据我的理解,它们会破坏char数组中的值,并且'\''与语句末尾的\冲突.

is causing issues. Ad per my understanding they break the values in char arrays and '\'' is conflicting with my \ in the end of the statement.

  • 我读到人们要求使用prepared statement,因为插入内容也发生了同样的问题,但是我的查询是动态的,所以我不能使用它.

  • I read that people ask to use prepared statement since same issue is happening with inserts, but my queries are dynamic so I cannot use that

我们不能说不可能在数据库中插入像dhruv\这样的值.我们可以直接通过控制台或json文件插入(由于Java需要转义单个\,而db \不是转义字符,这在Java中是困难的)

We cannot say that its is not possible to insert values like dhruv\ in database. We can insert directly by console or json files(btw its difficult by java since we need to escape single \ and for db \ is not escape character)

Cratedb最新版本具有

Cratedb in latest version has String Literals with C-Style Escape, but just to use this feature I cannot update my whole data base

那么有没有解决的办法?

So is there a way around for it?

还找到了类似的查询

select * from test where col1='dh\''ruv'

由于同样的原因也将无法正常工作.

will also not work due to same reason.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆