---
title: "List SQLite table columns in C#"
url: https://recursiveai.org/onelite/cookbook/list-sqlite-table-columns/
description: "List column names and declared types in C# with SQLite’s pragma_table_info. Use pragma_table_xinfo to include generated and hidden columns."
version: "1.0.0"
---

# List SQLite table columns in C#

**OneLite** is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLite) · [GitHub](https://github.com/recursive-ai-oss/onelite-csharp) · [Docs](https://recursiveai.org/onelite/docs/) · [Download example (.zip)](https://recursiveai.org/onelite/cookbook/list-sqlite-table-columns/list-sqlite-table-columns.zip)

```shell
dotnet add package RecursiveAI.OneLite
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneLite;
using var db =
    Database.OpenDefault(":memory:");
db.Execute($"CREATE TABLE t(id INT, txt TEXT)");
var columns = db.Query<string>($"""
    SELECT name || ' ' || type
    FROM pragma_table_info('t') ORDER BY cid
    """);
Console.WriteLine(string.Join('\n', columns));
```

```text
id INT
txt TEXT
```

The program creates table `t` in memory, so you don’t need a database file. If the table name doesn’t exist in the selected schema, the query returns no rows.

To inspect an existing file, replace the database open and `CREATE TABLE` lines with `using var db = Database.OpenReadOnly("app.db");` and use the name of a table in that file.

Unzip [the example](https://recursiveai.org/onelite/cookbook/list-sqlite-table-columns/list-sqlite-table-columns.zip), then `dotnet run`.

## Variations

**Include generated columns.** Replace the code after `using RecursiveAI.OneLite;` with the following. `pragma_table_xinfo` includes `body_size`, which `pragma_table_info` omits.

```csharp
using var db = Database.OpenDefault(":memory:");
db.Execute($"""
    CREATE TABLE notes(body TEXT,
      body_size INT GENERATED ALWAYS AS (length(body)) VIRTUAL)
    """);
foreach (var name in db.Query<string>($"""
    SELECT name FROM pragma_table_xinfo('notes') ORDER BY cid
    """))
    Console.WriteLine(name);
```

```text
body
body_size
```

## Related recipes

[Open an existing SQLite database](https://recursiveai.org/onelite/cookbook/open-existing-sqlite-database-csharp/) · [Join attached SQLite databases](https://recursiveai.org/onelite/cookbook/join-attached-sqlite-databases/) · [Query SQLite through ADO.NET](https://recursiveai.org/onelite/cookbook/sqlite-ado-net-csharp/)

---

## The short answer

Query `pragma_table_info('t')` and sort by `cid` for the columns’ table order. Select `name` for the names and `type` for the declared SQL types. The example joins those two fields into one string per column because `Query<string>` reads the first result field.

`pragma_table_info` is standard SQLite SQL and also works through `Microsoft.Data.Sqlite` configured with the [OneLite adapter](https://recursiveai.org/onelite/docs/adapter/).

## How do you get a list of column names from a SQLite table?

Use the table-valued form when you want to select or filter particular fields with SQL. You can also bind the table name as an argument. Once `t` exists, the following prints only its column names. In OneLite’s interpolated SQL, `{table}` binds as a SQL parameter, not as part of the query text. Leave it outside SQL quotes.

```csharp
string table = "t";
foreach (var name in db.Query<string>($"""
    SELECT name FROM pragma_table_info({table}) ORDER BY cid
    """))
    Console.WriteLine(name);
```

```text
id
txt
```

The optional second argument names a schema, such as `main` or the name you used in `ATTACH`. This lets you pick the right table when two database files have tables with the same name.

## How do you see a SQLite table’s schema, including generated columns?

Use `pragma_table_xinfo` if you need a complete column list rather than just ordinary columns. SQLite’s [pragma reference](https://sqlite.org/pragma.html#pragma_table_xinfo) explains the `hidden` field: `0` for ordinary columns, `1` for hidden virtual-table columns, and `2` or `3` for generated columns. Don’t filter to `hidden = 0` if the tool you’re building needs generated columns.

The `type` field describes the table declaration, not the type of each stored value. For a schema display, you can also select `"notnull"`, `dflt_value` and `pk` from the pragma row. `pk` is zero for a non-key column and otherwise gives its position within the primary key, which matters for composite keys.
