gpt4 book ai didi

java - 检索表名列表

转载 作者:行者123 更新时间:2023-12-04 03:56:38 24 4
gpt4 key购买 nike

我想获取数据库中表的列表。

The documentation of the tableList command说这个方法返回一个字符串列表,但事实并非如此。它实际上返回一个 TableList对象。

当我运行时..

r.db("test").tableList().run(conn);

我得到一个 Result<Object>对象作为结果,其中一个条目实际上是 ArrayList包含我想要的所有表名。

那么,这实际上是现在应该如何工作的吗:

Connection connection = r.connection().hostname(DEFAULT_HOSTNAME).port(DEFAULT_PORT).connect();

Object tableListObject = r.db(DEFAULT_DB_NAME).tableList().run(connection).single();

if (tableListObject instanceof Collection) {
List<?> tableList = new ArrayList<>((Collection<?>) tableListObject);

for(Object tableName : tableList) {
System.out.println(tableName);
}
}

对我来说似乎相当复杂,是否有官方/更好的方法来做到这一点?

我正在使用 RethinkDB Java 驱动程序版本 2.4.4。

最佳答案

您可以利用 run()允许您指定返回结果类的方法 - 在本例中为字符串数组:

Connection conn = r.connection().hostname("localhost").port(32769).connect();
List<?> tables = r.db("test").tableList().run(conn, ArrayList.class).first();
if (tables != null) {
tables.forEach(System.out::println);
}

对我来说,这会在我的测试数据库中打印以下内容:

movies
tv_shows

更新使用 List<?>正如@Johannes 所建议的。

关于java - 检索表名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63758220/

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