Kar*_*iam 12 java sql linux sql-server bcp
我试图执行以下代码来导出数据(myFileName.csv):
bcp "select * from DataBase.schema.TABLE_NAME" queryout tableData.csv -c -t, , -S [server] -U [user] -P '[password(with special characters)]' > LogFile.txt
Run Code Online (Sandbox Code Playgroud)
上面的代码在终端上工作正常.
相比之下,我使用java尝试了相同的代码.
File dir = new File("Mydirectory");
Path dataPath = Paths.get("tableData.csv");
List<String> val = new ArrayList();
val.add("bcp");
val.add("\"select * from " + [Database] + ".[Schema]." + table_name + "\"");
val.add("queryout");
val.add(dataPath.toString());
val.add("-c");
val.add("-t");
val.add(",");
val.add("-S");
val.add([server]);// ex: if Server is 10.0.0.1 then val.add("10.0.0.1");
val.add("-U");
val.add([user]); // ex: if User_name is TestA then val.add("TestA");
val.add("-P");
val.add([password(with special characters)]); // ex: if Password is !@#MyPassword*& then val.add("!@#MyPassword*&");
ProcessBuilder builder = new ProcessBuilder(val);
File logFile = new File("LogFile.txt");
System.out.println("BCP command :" + builder.command());
builder.redirectlogFile(logFile);
builder.directory(dir);
Process exec = builder.start();
System.out.println("BCP process completed : with errors :" + exec.waitFor());
System.out.println("BCP logFile :" + org.?apache.?commons.?io.FileUtils.readFileToString(logFile));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
BCP命令:[bcp,"select*from DataBase.schema.TABLE_NAME",queryout,tableData.csv,-c,-t ,,, -S,10.0.0.1,-U,TestA,-P,!@#MyPassword*]
BCP流程已完成:有错误:1
BCP logFile:正在启动副本... SQLState = S1000,NativeError = 0错误= [Microsoft] [SQL Server Native Client 11.0]无法解析列级排序规则
SQLState = 37000,NativeError = 102错误= [Microsoft] [SQL Server Native Client 11.0] [SQL Server]'select*from DataBase.schema.TABLE_NAME'附近的语法不正确.
通过查看错误我检查了整理Server
,Database
并且Table
所有看起来类似SQL_Latin1_General_CP1_CI_AS
Linux :
uname -mrs
Linux 3.10.0-327.10.1.el7.x86_64 x86_64
uname -a
Linux [domain] 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
ODBC Driver for linux:
isql --version
unixODBC 2.3.0
odbcinst -q -d -n "SQL Server Native Client 11.0"
[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Run Code Online (Sandbox Code Playgroud)
任何人都帮助我,我在这里做错了什么.
我尝试过创建 shell 脚本 \nits 工作正常。
\n\nFile dir = new File("Mydirectory"); \nPath dataPath = Paths.get("tableData.csv"); \nSStringBuilder strb = new StringBuilder();\nstrb.append("bcp ");\nstrb.append("\\"select " + column + " from " + credentials.getSchema() + ".dbo." + table_name + "\\" ");\nstrb.append("queryout ");\nstrb.append(dataPath.toString());\nstrb.append(" -c ");\nstrb.append("-t ");\nstrb.append(", ");\nstrb.append("-S ");\nstrb.append(credentials.getServer());\nstrb.append(" -U ");\nstrb.append(credentials.getUser());\nstrb.append(" -P ");\nstrb.append(credentials.getPassword());\n\nFile shellFile = new File(folderName + File.separator + "exec.sh");\n\ntry (FileOutputStream outShell = new FileOutputStream(shellFile)) {\n outShell.write(strb.toString().getBytes());\n outShell.flush();\n}\nshellFile.setExecutable(true, false);\nshellFile.setWritable(true, false);\nshellFile.setReadable(true, false);\nbuilder = new ProcessBuilder(shellFile.getAbsolutePath());\n\nFile logFile = new File("LogFile.txt");\nSystem.out.println("BCP command :" + builder.command());\nbuilder.redirectlogFile(logFile); builder.directory(dir); \nProcess exec = builder.start();\nSystem.out.println("BCP process completed : with errors :" + exec.waitFor());\nSystem.out.println("BCP logFile :" + org.\xe2\x80\x8bapache.\xe2\x80\x8bcommons.\xe2\x80\x8bio.FileUtils.readFileToString(logFile));\n
Run Code Online (Sandbox Code Playgroud)\n\n谢谢大家...
\n