' ” '和” ' ”在Java中..如何处理它们?

发布于 2024-09-11 03:09:15 字数 347 浏览 7 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浊酒尽余欢 2024-09-18 03:09:15

用反斜杠转义它们: " 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).

年少掌心 2024-09-18 03:09:15

在 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.

清风无影 2024-09-18 03:09:15

查看转义序列:

http://download .oracle.com/docs/cd/E17409_01/javase/tutorial/java/data/characters.html

你的最终字符串将如下所示:

"SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\' LINES TERMINATED BY '\n' ;"

另外,意识到“\\”将产生一个 \ 所以如果你真的想要两个斜杠,你需要“\\\\”,如果你想让它打印出\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:

"SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\' LINES TERMINATED BY '\n' ;"

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.

旧伤慢歌 2024-09-18 03:09:15

您还需要将反斜杠加倍,如果您需要编写\n。

You will need to also double the backslashes, f.e you will need to write \n.

情徒 2024-09-18 03:09:15

使用 \" 字符将 " 放入字符串中。

Use \" characters to put " in your string.

浅紫色的梦幻 2024-09-18 03:09:15

您需要使用 \ 转义内部字符串:

"Bob said \"hello world\" to his friends learning Java."

此外,字符串中的反斜杠也需要转义:

"This is a backslash: \\, so two backslashes would be \\\\"

如有疑问,请添加更多反斜杠。

You need to escape the inside string with \:

"Bob said \"hello world\" to his friends learning Java."

Also, the backslashes in your string need to be escaped too:

"This is a backslash: \\, so two backslashes would be \\\\"

When in doubt, add more backslashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文