dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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));
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, 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.
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);
body body_size
Related recipes
Open an existing SQLite database · Join attached SQLite databases · Query SQLite through ADO.NET
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.
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.
string table = "t"; foreach (var name in db.Query<string>($""" SELECT name FROM pragma_table_info({table}) ORDER BY cid """)) Console.WriteLine(name);
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 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.