gpt4 book ai didi

java - 从 Java 7 调用 SQLCMD

转载 作者:行者123 更新时间:2023-11-30 11:30:57 26 4
gpt4 key购买 nike

我在从 Java 执行 sqlcmd 时遇到错误。我的代码如下:

Process process = Runtime.getRuntime()
.exec("sqlcmd -m-1 -S localhost,1433 -d my_server -U sa
-P abc -Q \"select * from Assests where AssetID >
'G123204E4-1234-4A31-B37B-0092D68DA429'\" -k2 -W
-h-1 -s\",\" -o \"D://Assests.csv\"");

虽然从 sqlclient 运行 sqlcmd 它工作正常但是从 Java 它给出以下表达式错误:

Msg 4145, Level 15, State 1, Server SANDEEPB-PC, Line 1 An expression of non-boolean type specified in a context where a condition is expected, near 'AssetID'.

然而,当我将“>”更改为“=”时,它工作正常。

谁能告诉我为什么会这样,我能做些什么?

最佳答案

您应该使用 exec() 的字符串数组版本传递命令,请参阅: http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[])这将正确地转义所有参数。

参见 Java Runtime exec() fails to escape characters properly

应该是这样的(未测试);

Process process = Runtime.getRuntime()
.exec(new String[] {"sqlcmd", "-m", "-1", "-S", "localhost,1433", "-d", "my_server", "-U", "sa", "-P", "abc", "-Q", "select * from Assests where AssetID > 'G123204E4-1234-4A31-B37B-0092D68DA429'", "-k2", "-W", "-h", "-1", "-s", ",\"", "-o", "D://Assests.csv"});

用于 MySQL(已测试)

Process process = Runtime.getRuntime()
.exec(new String[] {"mysql", "-u", "root", "-ppassword","-e", "select 1<2"});

关于java - 从 Java 7 调用 SQLCMD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17491851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com