Braces bind values, not SQL text.
dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
Returns the bound value, apostrophe included. The full program shows how to bind values in INSERT and WHERE.
O'BrienThe 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.
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:
O'Brien Matches: 1
Variations
Bind more than one value. Each hole supplies one parameter.
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}"));
21Related recipes
Create a SQLite database · Use SQLite with ADO.NET · Run a SQL 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.
With OneLite’s SQLitePCLRaw 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 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.