简体   繁体   English

从Java使用\\ i命令在postgres中导入.sql文件

[英]import .sql file in postgres using \i command from java

I am trying to import database from .sql file in postgres using "\\i E:/dump.sql" command , its working fine from postgres command prompt but when i try the same from java it raise an error at "\\" , my code is 我正在尝试使用“ \\ i E:/dump.sql”命令从postgres中的.sql文件导入数据库,它可以从postgres命令提示符下正常工作,但是当我从java尝试同样的操作时,它会在“ \\”处引发错误,我代码是

connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/database","postgres", "passwd");
PreparedStatement ps3 = connection.prepareStatement("\\i E:/dump.sql");
boolean res3= ps3.execute();
System.out.println("imported succesfully .."+res3);

With the JDBC driver/interface you can only talk SQL, what you're trying is to issue PostgreSQL commandline tool ( psql ) specific commands. 使用JDBC驱动程序/接口,您只能使用SQL,而您要尝试的是发出PostgreSQL命令行工具( psql )特定命令。 That won't work. 那行不通。

If you insist on doing this, you could use the Runtime.getRuntime().exec(...) approach, something like 如果您坚持要这样做,则可以使用Runtime.getRuntime().exec(...)方法,例如

Runtime.getRuntime().exec( "psql -f dump.sql" );

Cheers, 干杯,

Long story short - you can't. 长话短说-您不能。 \\i is not PostgreSQL command (as in: PostgreSQL database engine). \\i不是PostgreSQL命令(如:PostgreSQL数据库引擎)。 It's command of psql - which is command line tool for interacting with database. 它是psql的命令psql是用于与数据库进行交互的命令行工具。

If you're connecting to database via JDBC you're not using psql, so you can't use its commands ( \\i , \\o and alike). 如果通过JDBC连接数据库,则说明您未使用psql,因此无法使用其命令( \\i\\o等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM