跳到内容

SHOW TABLES

在 Polars 中,`SHOW TABLES` 语句用于列出当前 `SQLContext` 中所有已注册的表。当您使用 `SQLContext` 注册一个 DataFrame 时,您会给它一个名称,该名称可在后续的 SQL 语句中引用该 DataFrame。`SHOW TABLES` 语句允许您查看所有已注册表的列表及其名称。

Polars 中 `SHOW TABLES` 语句的语法如下

SHOW TABLES

以下是在 Polars 中使用 `SHOW TABLES` 语句的示例

register · execute

# Create some DataFrames and register them with the SQLContext
df1 = pl.LazyFrame(
    {
        "name": ["Alice", "Bob", "Charlie", "David"],
        "age": [25, 30, 35, 40],
    }
)
df2 = pl.LazyFrame(
    {
        "name": ["Ellen", "Frank", "Gina", "Henry"],
        "age": [45, 50, 55, 60],
    }
)
ctx = pl.SQLContext(mytable1=df1, mytable2=df2)

tables = ctx.execute("SHOW TABLES", eager=True)

print(tables)

shape: (2, 1)
┌──────────┐
│ name     │
│ ---      │
│ str      │
╞══════════╡
│ mytable1 │
│ mytable2 │
└──────────┘

在此示例中,我们创建了两个 DataFrame 并使用不同的名称将它们注册到 `SQLContext` 中。然后,我们使用 `SQLContext` 对象的 `execute()` 方法执行 `SHOW TABLES` 语句,该方法返回一个 DataFrame,其中包含所有已注册的表及其名称的列表。最后,使用 `print()` 函数打印结果 DataFrame。

请注意,`SHOW TABLES` 语句仅列出已在当前 `SQLContext` 中注册的表。如果您使用不同的 `SQLContext` 或在不同的 Python 会话中注册 DataFrame,它将不会出现在 `SHOW TABLES` 返回的表列表中。