实际上我已经用谷歌搜索了一下,我需要对应的SELECT命令来跟随下面的PostgreSQL shell命令:
\dt schemaname.*
我设法用以下代码获取所有数据库:
Statement statement = (Statement) connection.createStatement(); ResultSet rs = statement .executeQuery("SELECT datname FROM pg_database"); while (rs.next()) { System.out.println("DB Name : " + rs.getString(1)); //i need another while here to list tables //inside the selected database }
我尝试了以下声明,但没有运气:
statement.executeQuery("SELECT table_schema,table_name FROM " + rs.getString(1) + " ORDER BY table_schema,table_name");
这是我得到的错误:
org.postgresql.util.PSQLException: ERROR: relation "template1" does not exist Position: 37 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254) at com.isiran.rayten.rg.db.bare.wrapper.PGWrap.main(PGWrap.java:64)
要列出数据库中的所有表,您必须读取表,pg_catalog.pg_tables 但是不幸的是,您必须登录数据库。所以在你写评论的地方
pg_catalog.pg_tables
//i need another while here to list tables //inside the selected database
在循环表之前,您需要登录该数据库。