---
title: "sqflite Database Instrumentation"
description: "Learn more about the Sentry sqflite Database Instrumentation for the Flutter SDK."
url: https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation/
---

# sqflite Database Instrumentation | Sentry for Flutter

This feature is currently in Beta. Beta features are still in progress and may have bugs. We recognize the irony.

*(New in version 7.2.0)*

The [sentry\_sqflite](https://pub.dev/packages/sentry_sqflite) package provides `sqflite` support for database instrumentation and allows you to track the performance of your queries.

## [Instrumentation Behaviour](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#instrumentation-behaviour)

* The created spans will be attached to the transaction on the scope - if no transaction is on the scope the sqflite span will not be sent to Sentry.
* The spans' `operation` are either `db`, `db.sql.execute`, `db.sql.query` or `db.sql.transaction`. Its `description` is the SQL statement using placeholders instead of its values due to the possibility of containing PII.

## [Prerequisites](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#prerequisites)

Before starting, ensure:

1. The Sentry Flutter SDK is initialized. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter.md#configure).
2. Tracing is set up. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter/tracing.md).

## [Install](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#install)

Add the `sentry_sqflite` dependency to install the sqflite database instrumentation.

`pubspec.yaml`

```yml
dependencies:
  sentry_flutter: ^9.16.1
  sentry_sqflite: ^9.16.1
  sqflite: ^2.0.0
```

## [Configure](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#configure)

There are multiple ways to configure the sqflite database instrumentation:

### [Global `databaseFactory`](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#global-databasefactory)

By using the global `databaseFactory`, (which is used by the `openDatabase` method). With this approach, every call to `openDatabase` will be instrumented, including from other packages:

```dart
import 'package:sentry_sqflite/sentry_sqflite.dart';
import 'package:sqflite/sqflite.dart';

databaseFactory = SentrySqfliteDatabaseFactory();
final database = await openDatabase('path/to/db');
```

If you're using the `FFI` factories, you can instrument the `SentrySqfliteDatabaseFactory` with its factory:

```dart
import 'package:sentry_sqflite/sentry_sqflite.dart';
import 'package:sqflite/sqflite.dart';

databaseFactory = SentrySqfliteDatabaseFactory(databaseFactory: databaseFactoryFfi);
final database = await openDatabase('path/to/db');
```

### [`openDatabaseWithSentry` Wrapper](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#opendatabasewithsentry-wrapper)

Use the `openDatabaseWithSentry` wrapper - with this approach only the database opened with this method will be instrumented:

```dart
import 'package:sentry_sqflite/sentry_sqflite.dart';
import 'package:sqflite/sqflite.dart';

final database = await openDatabaseWithSentry('path/to/db');
// or final database = await openReadOnlyDatabaseWithSentry('path/to/db');
```

### [`SentryDatabase` Wrapper](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#sentrydatabase-wrapper)

Use the `SentryDatabase` wrapper which can be used to instrument any `Database` instance:

```dart
import 'package:sentry_sqflite/sentry_sqflite.dart';
import 'package:sqflite/sqflite.dart';

final database = await openDatabase('path/to/db');
final sentryDatabase = SentryDatabase(database);
```

## [Verify](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#verify)

### [1. Execute the Code](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#1-execute-the-code)

```dart
import 'package:sentry_sqflite/sentry_sqflite.dart';
import 'package:sqflite/sqflite.dart';

Future<void> sqfliteTest() async {
  final tr = Sentry.startTransaction(
    'sqfliteTest',
    'db',
    bindToScope: true,
  );

  // You can also use the other configuration methods
  final db = await openDatabaseWithSentry(inMemoryDatabasePath);

  await db.execute('''CREATE TABLE Product (id INTEGER PRIMARY KEY,title TEXT)''');
  final dbTitles = <String>[];
  for (int i = 1; i <= 20; i++) {
    final title = 'Product $i';
    dbTitles.add(title);
    await db.insert('Product', <String, Object?>{'title': title});
  }

  await db.query('Product');

  await db.transaction((txn) async {
    await txn
    .insert('Product', <String, Object?>{'title': 'Product Another one'});
    await txn.delete('Product',
    where: 'title = ?', whereArgs: ['Product Another one']);
  });

  await db.delete('Product', where: 'title = ?', whereArgs: ['Product 1']);

  await db.close();

  await tr.finish(status: const SpanStatus.ok());
}
```

### [2. View the Transaction on Sentry](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/sqflite-instrumentation.md#2-view-the-transaction-on-sentry)

To view the recorded transaction, log into [sentry.io](https://sentry.io) and open your project (via main navigation menu Insights > Projects). Clicking **View Transactions** (in *Quick Links* section) will open a page with transactions, where you can select the just recorded transaction with the name `sqfliteTest`.
