Gå til indhold

Get started with the SQLite integrations

Dette indhold er ikke tilgængeligt i dit sprog endnu.

⭐ Community Toolkit SQLite logo

SQLite is a lightweight, serverless, self-contained SQL database engine that is widely used for local data storage in applications. The Aspire SQLite integration provides a way to use SQLite databases within your Aspire applications, and access them via the Microsoft.Data.Sqlite client.

In this introduction, you’ll see how to install and use the Aspire SQLite integrations in a simple configuration. If you already have this knowledge, see SQLite Hosting integration for full reference details.

To begin, install the Aspire SQLite Hosting integration in your Aspire AppHost project. This integration allows you to create and manage SQLite database instances from your Aspire hosting projects:

Aspire CLI — Tilføj CommunityToolkit.Aspire.Hosting.SQLite-pakke
aspire add sqlite

Aspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:

Aspire CLI — Eksempel output
Select an integration to add:
> sqlite (CommunityToolkit.Aspire.Hosting.SQLite)
> Other results listed as selectable options...

Next, in the AppHost project, create an instance of a SQLite database resource, then pass the database to the consuming client projects:

Select your programming language to get started
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sqlite = builder.AddSqlite("sqlite");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(sqlite);
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sqlite = builder.AddSqlite("sqlite");
var exampleProject = builder.AddUvicornApp("api", "./api", "main.app")
.WithExternalHttpEndpoints()
.WithReference(sqlite);
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sqlite = builder.AddSqlite("sqlite");
var api = builder.AddNodeApp("api", "./api", scriptPath: "index.js")
.WithExternalHttpEndpoints()
.WithReference(sqlite);

Now that the hosting integration is ready, the next step is to install and configure the client integration in any projects that need to use it.

In each of these consuming client projects, install the Aspire SQLite client integration:

.NET CLI — Add CommunityToolkit.Aspire.Microsoft.Data.Sqlite package
dotnet add package CommunityToolkit.Aspire.Microsoft.Data.Sqlite

In the Program.cs file of your client-consuming project, call the AddSqliteConnection extension method on any IHostApplicationBuilder to register a SqliteConnection for use via the dependency injection container. The method takes a connection name parameter.

C# — Program.cs
builder.AddSqliteConnection("sqlite");

To interact with SQLite databases in your Python consuming projects, you can use the built-in sqlite3 module (included in Python’s standard library). No additional installation is required:

Python - Import sqlite3
import sqlite3
import os

To interact with SQLite databases in your JavaScript consuming projects, you need to include a SQLite client library. A popular option is sqlite3. You can install this library using npm:

Terminal window
npm install sqlite3

Ensure that you import sqlite3 in code files that interact with the database:

JavaScript - Import sqlite3
import sqlite3 from 'sqlite3';

In the AppHost, when you used the WithReference method to pass a SQLite database resource to a consuming client project, Aspire injects the data source configuration property that you can use in the consuming project.

Aspire exposes the property as an environment variable named [RESOURCE]_DATASOURCE. For instance, the data source of a resource called sqlite becomes SQLITE_DATASOURCE.

Use the GetValue() method to obtain this environment variable in consuming projects:

C# - Obtain configuration properties
string sqldatasource = builder.Configuration.GetValue<string>("SQLITE_DATASOURCE");

Use the os.getenv() method to obtain this environment variable in consuming projects:

Python - Obtain configuration properties
connection_string = os.getenv("SQLITE_DATASOURCE")

Use the process.env method to obtain this environment variable in consuming projects:

JavaScript - Obtain configuration properties
const connectionString = process.env.SQLITE_DATASOURCE;

Now that you’ve added SqliteConnection to the builder in the consuming project, you can use the SQLite resource to get and store data. Get the SqliteConnection instance using dependency injection. For example, to retrieve your connection object from an example service define it as a constructor parameter and ensure the ExampleService class is registered with the dependency injection container:

C# — ExampleService.cs
public class ExampleService(SqliteConnection connection)
{
// Use connection to query the database...
}

Having obtained the connection, you can work with the SQLite database as you would in any other C# application.

Use the information you have obtained about the SQLite resource to connect to the database. Here is an example of how to connect using sqlite3:

Python - Connect to SQLite
connection = sqlite3.connect(connection_string)
cursor = connection.cursor()

Having obtained the connection, you can work with the SQLite database as you would in any other Python application.

Use the information you have obtained about the SQLite resource to connect to the database. Here is an example of how to connect using sqlite3:

JavaScript - Connect to SQLite
const db = new sqlite3.Database(connectionString);

Having obtained the connection, you can work with the SQLite database as you would in any other JavaScript application.

Now, that you have an Aspire app with SQLite integrations up and running, you can use the following reference documents to learn how to configure and interact with the SQLite resources: