sqlite3 0.0.1+1

Sample code dart:ffi #

This is an illustrative sample for how to use dart:ffi.

Building and Running this Sample #

Building and running this sample is done through pub. Running pub get and pub run example/main should produce the following output.

$ pub get
Resolving dependencies... (6.8s)
+ analyzer 0.35.4
...
+ yaml 2.1.15
Downloading analyzer 0.35.4...
Downloading kernel 0.3.14...
Downloading front_end 0.1.14...
Changed 47 dependencies!
Precompiling executables... (18.0s)
Precompiled test:test.

$ pub run example/main
1 Chocolade chip cookie Chocolade cookie foo
2 Ginger cookie null 42
3 Cinnamon roll null null
1 Chocolade chip cookie Chocolade cookie foo
2 Ginger cookie null 42
expected exception on accessing result data after close: The result has already been closed.
expected this query to fail: no such column: non_existing_column (Code 1: SQL logic error)

Tutorial #

A tutorial walking through the code is available in docs/sqlite-tutorial.md. For information on how to use this package within a Flutter app, see docs/android.md. (Note: iOS is not yet supported).

example/main.dart

// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import "../lib/sqlite.dart";

void main() {
  Database d = Database("test.db");
  d.execute("drop table if exists Cookies;");
  d.execute("""
      create table Cookies (
        id integer primary key,
        name text not null,
        alternative_name text
      );""");
  d.execute("""
      insert into Cookies (id, name, alternative_name)
      values
        (1,'Chocolade chip cookie', 'Chocolade cookie'),
        (2,'Ginger cookie', null),
        (3,'Cinnamon roll', null)
      ;""");
  Result result = d.query("""
      select
        id,
        name,
        alternative_name,
        case
          when id=1 then 'foo'
          when id=2 then 42
          when id=3 then null
        end as multi_typed_column
      from Cookies
      ;""");
  for (Row r in result) {
    int id = r.readColumnAsInt("id");
    String name = r.readColumnByIndex(1);
    String alternativeName = r.readColumn("alternative_name");
    dynamic multiTypedValue = r.readColumn("multi_typed_column");
    print("$id $name $alternativeName $multiTypedValue");
  }
  result = d.query("""
      select
        id,
        name,
        alternative_name,
        case
          when id=1 then 'foo'
          when id=2 then 42
          when id=3 then null
        end as multi_typed_column
      from Cookies
      ;""");
  for (Row r in result) {
    int id = r.readColumnAsInt("id");
    String name = r.readColumnByIndex(1);
    String alternativeName = r.readColumn("alternative_name");
    dynamic multiTypedValue = r.readColumn("multi_typed_column");
    print("$id $name $alternativeName $multiTypedValue");
    if (id == 2) {
      result.close();
      break;
    }
  }
  try {
    result.iterator.moveNext();
  } on SQLiteException catch (e) {
    print("expected exception on accessing result data after close: $e");
  }
  try {
    d.query("""
      select
        id,
        non_existing_column
      from Cookies
      ;""");
  } on SQLiteException catch (e) {
    print("expected this query to fail: $e");
  }
  d.execute("drop table Cookies;");
  d.close();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sqlite3: ^0.0.1+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sqlite3/sqlite.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
95
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Feb 12, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/database.dart. (-4.41 points)

Analysis of lib/src/database.dart reported 9 hints, including:

line 127 col 18: The value of the field '_database' isn't used.

line 129 col 28: The value of the field '_statement' isn't used.

line 130 col 26: The value of the field '_columnIndices' isn't used.

line 132 col 7: The value of the field '_currentRow' isn't used.

line 132 col 7: Don't explicitly initialize variables to null.

Fix lib/src/ffi/dylib_utils.dart. (-0.50 points)

Analysis of lib/src/ffi/dylib_utils.dart reported 1 hint:

line 11 col 5: DO use curly braces for all flow control structures.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
ffi ^0.1.3 0.1.3
Dev dependencies
test ^1.5.3