---
title: "SQLite parameters with C# interpolation"
url: https://recursiveai.org/onelite/cookbook/bind-sql-parameters-with-interpolated-strings/
description: "Pass C# interpolated strings to OneLite to bind SQLite query parameters. See how to insert and filter text with an apostrophe and keep identifiers fixed."
version: "1.0.0"
---

# SQLite parameters with C# interpolation

**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/bind-sql-parameters-with-interpolated-strings/bind-sql-parameters-with-interpolated-strings.zip)

Braces bind values, not SQL text.

```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;
await using var db =
    await Database.OpenAsync(":memory:");
var name = "O'Brien";
Console.WriteLine(await db.ScalarAsync<string>(
    $"SELECT {name}"));
```

Unzip [the example](https://recursiveai.org/onelite/cookbook/bind-sql-parameters-with-interpolated-strings/bind-sql-parameters-with-interpolated-strings.zip), then `dotnet run`.

Returns the bound value, apostrophe included. The full program shows how to bind values in `INSERT` and `WHERE`.

```text
O'Brien
```

## The full program

The download uses the same value in an `INSERT` and a `WHERE` filter. It creates both contacts in memory and reads back only the matching contact. Don’t put SQL quotes around `{name}`: OneLite supplies the parameter, and SQL quotes turn a placeholder into literal text.

```csharp
using RecursiveAI.OneLite;

await using var db = await Database.OpenAsync(":memory:");
await db.ExecuteAsync($"CREATE TABLE contacts(name TEXT NOT NULL)");
var name = "O'Brien";
await db.ExecuteAsync($"INSERT INTO contacts(name) VALUES ({name})");
await db.ExecuteAsync($"INSERT INTO contacts(name) VALUES ({"Mina"})");

var matches = await db.QueryAsync<string>(
    $"SELECT name FROM contacts WHERE name = {name}");
Console.WriteLine(string.Join(", ", matches));
Console.WriteLine($"Matches: {matches.Count}");
```

`dotnet run` prints:

```text
O'Brien
Matches: 1
```

## Variations

**Bind more than one value.** Each hole supplies one parameter.

```csharp
using RecursiveAI.OneLite;

await using var db =
    await Database.OpenAsync(":memory:");
int price = 7, quantity = 3;
Console.WriteLine(await db.ScalarAsync<long>(
    $"SELECT {price} * {quantity}"));
```

```text
21
```

## Related recipes

[Create a SQLite database](https://recursiveai.org/onelite/cookbook/create-sqlite-database-csharp/) · [Use SQLite with ADO.NET](https://recursiveai.org/onelite/cookbook/sqlite-ado-net-csharp/) · [Run a SQL command batch](https://recursiveai.org/onelite/cookbook/execute-sqlite-command-batch/)

---

## The short answer

With OneLite, write `db.QueryAsync<string>($"SELECT name FROM contacts WHERE name = {name}")`. OneLite sends `SELECT name FROM contacts WHERE name = ?1` to the engine and binds `name` separately; `?1` is SQLite’s numbered placeholder for parameter 1. The `$` alone doesn’t do that: this behavior comes from passing the interpolation to a OneLite query method. The quote in `O'Brien` is part of the value, not SQL syntax.

## How do you parameterize a SQL query in C#?

Keep the query structure in the literal and put values in `{}` where SQL expects expressions. The full program does this twice: once in `VALUES ({name})` to store the name, and once in `WHERE name = {name}` to look it up. The parentheses in `VALUES` are part of SQL; the braces belong to C#. You don’t need extra SQL quotes around the hole or manual escaping for the apostrophe.

Use `ExecuteAsync` for a change, `ScalarAsync<T>` when you want the first column of the first row, or `QueryAsync<T>` when you need a list. All three bind values in the interpolation. If you assign the interpolation to a regular `string` first, C# formats its values into text and loses their separation from the SQL. OneLite’s query methods don’t accept that ordinary `string` in place of interpolated SQL. `ExecuteScript` takes literal script text instead and does not bind parameters; keep untrusted text out of scripts. See [OneLite’s parameter rules](https://recursiveai.org/onelite/docs/happy-path/#-binds-parameters).

With OneLite’s [SQLitePCLRaw adapter](https://recursiveai.org/onelite/docs/adapter/), interpolating `$"..."` into `SqliteCommand.CommandText` does not bind values; use a placeholder such as `$name` and add the value with `command.Parameters.AddWithValue("$name", name)`.

## Can SQLite parameters be table or column names?

No. A parameter is a value, such as a string, number, blob or null. Table names, column names, operators and keywords are SQL syntax and must already be present when the statement is parsed. `SELECT {name}` returns the bound value for `name`; it doesn’t select a column whose identifier happens to match that value. Putting a hole after `FROM` can’t select a table by name either. The [SQLite expression reference](https://www.sqlite.org/lang_expr.html#parameters) describes where parameters fit.

If the reader can choose a sort field or table, map the choice to one of a small number of complete, fixed statements. Bind the filter values separately in each statement. The same rule applies to a list: an interpolation hole represents one value, not a comma-separated fragment to paste into `IN (…)`. Keep the selection of SQL syntax in your code and use parameters for the data you pass into it.
