Flutter中使用sqlite

7,001 阅读1分钟

sqflite使用

引入插件

在pubspec.yaml文件中添加path_provider插件,2019年2月18号最新版本为1.1.0:

dependencies:
  flutter:
    sdk: flutter
  #sqflite插件
  sqflite: ^1.1.0

执行 flutter packages get 下载插件。

数据库操作方法介绍

1. 创建数据库文件和对应的表
// 获取数据库文件的存储路径
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'demo.db');

//根据数据库文件路径和数据库版本号创建数据库表
    db = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute('''
          CREATE TABLE $tableBook (
            $columnId INTEGER PRIMARY KEY, 
            $columnName TEXT, 
            $columnAuthor TEXT, 
            $columnPrice REAL, 
            $columnPublishingHouse TEXT)
          ''');
    });
2. CRUD操作实现
  // 插入一条书籍数据
  Future<Book> insert(Book book) async {
    book.id = await db.insert(tableBook, book.toMap());
    return book;
  }

  // 查找所有书籍信息
  Future<List<Book>> queryAll() async {
    List<Map> maps = await db.query(tableBook, columns: [
      columnId,
      columnName,
      columnAuthor,
      columnPrice,
      columnPublishingHouse
    ]);

    if (maps == null || maps.length == 0) {
      return null;
    }

    List<Book> books = [];
    for (int i = 0; i < maps.length; i++) {
      books.add(Book.fromMap(maps[i]));
    }

    return books;
  }

  // 根据ID查找书籍信息
  Future<Book> getBook(int id) async {
    List<Map> maps = await db.query(tableBook,
        columns: [
          columnId,
          columnName,
          columnAuthor,
          columnPrice,
          columnPublishingHouse
        ],
        where: '$columnId = ?',
        whereArgs: [id]);
    if (maps.length > 0) {
      return Book.fromMap(maps.first);
    }
    return null;
  }

  // 根据ID删除书籍信息
  Future<int> delete(int id) async {
    return await db.delete(tableBook, where: '$columnId = ?', whereArgs: [id]);
  }

  // 更新书籍信息
  Future<int> update(Book book) async {
    return await db.update(tableBook, book.toMap(),
        where: '$columnId = ?', whereArgs: [book.id]);
  }
3. 关闭数据库

数据库对象使用完之后要在适当的时候关闭掉,可在helper类中实现以下方法。

Future close() async => db.close();

github demo代码地址:github.com/xinwii/flut…