如何更新数据库项中的特定值?

时间:2019-09-05 05:03:41

标签: database flutter sqflite

这是一个待办事项列表应用程序,使用数据库来使用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]);
  }

1 个答案:

答案 0 :(得分:0)

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

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

{
    "type": "record",
    "name": "Interactions",
    "namespace": "com.amazonaws.personalize.schema",
    "fields": [
        {
            "name": "InvoiceNo",
            "type": "int"
        },
        {
            "name": "StockCode",
            "type": "int"
        },
        {
            "name": "Description",
            "type": "long"
        },
        {
            "name": "Quantity",
            "type": "string"
        },
        {
            "name": "InvoiceDate",
            "type": "string"
        },
        {
            "name": "UnitPrice",
            "type": "string"
        },
        {
            "name": "CustomerID",
            "type": "string"
        },
        {
            "name": "CustomerID",
            "type": "string"
        },
        {
            "name": "Country",
            "type": "string"
        }
    ],
    "version": "1.0"
}

在您的示例中,您更新TodoItem,并且TodoItem包含isDone字段。
并且您的语法正确

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

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

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

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;
  }

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

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,
));