flutter开发实战-本地SQLite数据库存储
正在编写一个需要持久化且查询大量本地设备数据的 app,可考虑采用数据库。相比于其他本地持久化方案来说,数据库能够提供更为迅速的插入、更新、查询功能。这里需要用到sqflite package 来使用 SQLite 数据库
预览图
一、引入sqflite
在工程的pubspec.yaml中引入插件
# sqflite sqflite: ^2.2.8+4
二、使用sqflite
使用 sqflite 实现插入,读取,更新,删除数据。
- 打开数据库
Future
openDB(BuildContext context) async { // Open the database and store the reference. database = await openDatabase( // Set the path to the database. Note: Using the `join` function from the // `path` package is best practice to ensure the path is correctly // constructed for each platform. join(await getDatabasesPath(), 'doggie_database.db'), // When the database is first created, create a table to store dogs. onCreate: (db, version) { // Run the CREATE TABLE statement on the database. return db.execute( 'CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)', ); }, version: 1, ); } - 插入一条记录
Future
insertDB(BuildContext context) async { dogId++; // Create a Dog and add it to the dogs table var fido = Dog( id: dogId, name: 'Fido', age: 35, ); // Get a reference to the database. final db = await database; // Insert the Dog into the correct table. You might also specify the // `conflictAlgorithm` to use in case the same dog is inserted twice. // // In this case, replace any previous data. await db?.insert( 'dogs', fido.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } - 更新一条记录
Future
updateDog(Dog dog) async { // Get a reference to the database. final db = await database; // Update the given Dog. await db?.update( 'dogs', dog.toMap(), // Ensure that the Dog has a matching id. where: 'id = ?', // Pass the Dog's id as a whereArg to prevent SQL injection. whereArgs: [dog.id], ); } - 删除一条记录
Future
deleteDog(int id) async { // Get a reference to the database. final db = await database; // Remove the Dog from the database. await db?.delete( 'dogs', // Use a `where` clause to delete a specific dog. where: 'id = ?', // Pass the Dog's id as a whereArg to prevent SQL injection. whereArgs: [id], ); } - 获取存储记录
// A method that retrieves all the dogs from the dogs table. Future
- > dogs() async {
// Get a reference to the database.
final db = await database;
// Query the table for all the dogs.
final List
完整代码如下
import 'package:flutter/material.dart'; import 'dart:async'; import 'package:flutter/widgets.dart'; import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; class SqliteDemoPage extends StatefulWidget { const SqliteDemoPage({super.key}); @override State
createState() => _SqliteDemoPageState(); } class _SqliteDemoPageState extends State { Database? database; int dogId = 0; Future openDB(BuildContext context) async { // Open the database and store the reference. database = await openDatabase( // Set the path to the database. Note: Using the `join` function from the // `path` package is best practice to ensure the path is correctly // constructed for each platform. join(await getDatabasesPath(), 'doggie_database.db'), // When the database is first created, create a table to store dogs. onCreate: (db, version) { // Run the CREATE TABLE statement on the database. return db.execute( 'CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)', ); }, version: 1, ); } // A method that retrieves all the dogs from the dogs table. Future - > dogs() async {
// Get a reference to the database.
final db = await database;
// Query the table for all the dogs.
final List
三、小结
flutter开发实战-本地SQLite数据库存储
学习记录,每天不停进步。
- 获取存储记录
- 删除一条记录
- 更新一条记录
- 插入一条记录
还没有评论,来说两句吧...