' ” '和” ' ”在Java中..如何处理它们?
我用 JAVA 写这个:
stmt.executeQuery("SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' ;");
这必须写在“”之间,如图所示,因为它必须是一个字符串。但是,当我运行代码时,它显示“未闭合的字符串和字符横向”。我知道,但是如何不让编译器被内部语句的“和”所混淆?我希望编译器将它们视为正常的字符串而不是Java操作。
请帮助!
I'm writing this in JAVA :
stmt.executeQuery("SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' ;");
This is has to be written between " " as show because It has to be a String. However, when I run the code it says "Unclosed String and Character laterals". I know, but how to not let the compiler be confused by the " and ' which are part of the inner statement ? I want the compiler to consider them normal Strings not Java operations.
Plz Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
用反斜杠转义它们:
" Hello \"world\"!"
我不确定它在这种特殊情况下是否有帮助,但尝试一个PreparedStatement:它通常可以让你不必担心关于引用论点。 (我还没有看到它用于这种非 CRUD SQL,所以我不确定它会有帮助)。
Escape them with a backslash:
" Hello \"world\"!"
I'm not sure if it will help in this particular case, but try a PreparedStatement: it often allows you to not have to worry about quoting of arguments. (I haven't seen it used for this sort of non-CRUD SQL, so I'm not sure it will help).
在 Java 中,字符串用双引号表示,因此您不必担心字符串中的单引号。但是,您需要通过使用反斜杠 \ 转义查询中的双引号。
这应该是正确的:
stmt.executeQuery("SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' 字段终止于 ',' 可选地包含于 '\"' 转义于 '\\\\' 行终止于 '\\n ' ;");
编辑:您还需要转义查询中现有的反斜杠。上面的行应该可以工作。
In Java, strings are denoted with double quotations, so you shouldn't need to worry about the single quotations in your string. However, you need to escape the double quotations in your query by escaping them with a backslash \.
This should be correct:
stmt.executeQuery("SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' ;");
Edit: You also needed to escape the existing backslashes in your query. The above line should work.
查看转义序列:
http://download .oracle.com/docs/cd/E17409_01/javase/tutorial/java/data/characters.html
你的最终字符串将如下所示:
另外,意识到“\\”将产生一个 \ 所以如果你真的想要两个斜杠,你需要“\\\\”,如果你想让它打印出\n,你需要输入“\\n”等。
Look at Escape Sequences:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/data/characters.html
Your final string will look like this:
Also, realizes that "\\" will produce a \ so if you really want two slashes, you'll need "\\\\", and if you want it to print out \n, you need to type "\\n" etc.
您还需要将反斜杠加倍,如果您需要编写\n。
You will need to also double the backslashes, f.e you will need to write \n.
使用
\"
字符将"
放入字符串中。Use
\"
characters to put"
in your string.您需要使用
\
转义内部字符串:此外,字符串中的反斜杠也需要转义:
如有疑问,请添加更多反斜杠。
You need to escape the inside string with
\
:Also, the backslashes in your string need to be escaped too:
When in doubt, add more backslashes.