0

这是一个待办事项列表应用程序,使用数据库来使用 CRUD 功能。当用户执行特定操作(例如点击或滑动)时,我想将任务标记为完成。我对数据库有点陌生,不确定如何使用更新项来更新任务的状态(已完成或未完成)。

我能否获得关于在_isDone不从数据库中删除任务的情况下更新值的正确方法的建议

试图搜索待办事项列表应用程序的其他示例,但他们删除了已完成的任务而不是将其标记为已完成

数据库代码 -

 //This is the database

  String _itemName;
  String _dateCreated;
  int _id;
  bool _isDone;


  TodoItem(this._itemName, this._dateCreated, [this._isDone]);

  TodoItem.map(dynamic obj) {
    this._itemName = obj["itemName"];
    this._dateCreated = obj["dateCreated"];
    this._id = obj["id"];
    this._isDone = obj["isDone"];
  }


  String get itemName => _itemName;
  String get dateCreated => _dateCreated;
  int get id => _id;
  bool get isDone => _isDone;
  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    map["itemName"] = _itemName;
    map["dateCreated"] = _dateCreated;

    if (_id != null) {
      map["id"] = _id;
    }

    return map;
  }

  TodoItem.fromMap(Map<String, dynamic> map) {
    this._itemName = map["itemName"];
    this._dateCreated = map["dateCreated"];
    this._id = map["id"];
    this._isDone = map["isDone"];
  }

更新项目功能 -

  Future<int> updateItem(TodoItem item) async {
    var dbClient = await db;
    return await dbClient.update("$tableName", item.toMap(),
        where: "$columnId = ?", whereArgs: [item.id]);
  }
4

1 回答 1

0

请参考本文档https://medium.com/flutter-community/using-sqlite-in-flutter-187c1a82e8b

在下面的 sqlite 示例中,通过此处的“id”键更新整个记录

updateClient(Client newClient) async {
    final db = await database;
    var res = await db.update("Client", newClient.toMap(),
        where: "id = ?", whereArgs: [newClient.id]);
    return res;
  }

在您的示例中,您更新 TodoItem 和 TodoItem 包括 isDone 字段。
你的语法是正确的

另一个参考文档https://www.developerlibs.com/2018/07/flutter-sqlite-database-example.html 此示例的代码片段

Future<bool> update(User user) async {
    var dbClient = await db;
    int res =   await dbClient.update("User", user.toMap(),
        where: "id = ?", whereArgs: <int>[user.id]);
    return res > 0 ? true : false;
  }

关于使用 sqlite的官方文档https://flutter.dev/docs/cookbook/persistence/sqlite 官方文档代码片段

Future<void> 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],
  );
}

// Update Fido's age.
await updateDog(Dog(
  id: 0, 
  name: 'Fido', 
  age: 42,
));

您可以构建一个数据库助手作为示例并包含您的更新功能

import 'dart:async';
import 'dart:io' as io;

import 'package:flutter_database/database/model/user.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseHelper {
  static final DatabaseHelper _instance = new DatabaseHelper.internal();
  factory DatabaseHelper() => _instance;
  static Database _db;

  Future<Database> get db async {
    if (_db != null) return _db;
    _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "main.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;
  }

  void _onCreate(Database db, int version) async {
    // When creating the db, create the table
    await db.execute(
        "CREATE TABLE User(id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT, dob TEXT)");
  }

  Future<int> saveUser(User user) async {
    var dbClient = await db;
    int res = await dbClient.insert("User", user.toMap());
    return res;
  }

  Future<List<User>> getUser() async {
    var dbClient = await db;
    List<Map> list = await dbClient.rawQuery('SELECT * FROM User');
    List<User> employees = new List();
    for (int i = 0; i < list.length; i++) {
      var user =
          new User(list[i]["firstname"], list[i]["lastname"], list[i]["dob"]);
      user.setUserId(list[i]["id"]);
      employees.add(user);
    }
    print(employees.length);
    return employees;
  }

  Future<int> deleteUsers(User user) async {
    var dbClient = await db;

    int res =
        await dbClient.rawDelete('DELETE FROM User WHERE id = ?', [user.id]);
    return res;
  }

  Future<bool> update(User user) async {
    var dbClient = await db;
    int res =   await dbClient.update("User", user.toMap(),
        where: "id = ?", whereArgs: <int>[user.id]);
    return res > 0 ? true : false;
  }
}
于 2019-09-05T05:38:21.913 回答