CRUD 在 Flutter 中使用 SQFlite

huangapple go评论75阅读模式
英文:

CRUD with SQFlite in Flutter

问题

要更新待办事项,您可以使用以下代码示例:

final db = await _getDatabase();
await db.update(
  'todo',
  {
    'description': updatedTodo.description,
    'isDone': updatedTodo.isDone == true ? 'true' : 'false',
  },
  where: 'id = ?',
  whereArgs: [updatedTodo.id],
);

这段代码会更新名为 "todo" 的表中与指定ID匹配的待办事项的描述和完成状态。确保将 updatedTodo 替换为包含更新信息的实际待办事项对象。

英文:

hello I have made an app that can write and delete files in SQFlite. But I need help with updating an existing entry. I will paste my code below.

For Create I used -
db.insert('todo',{'id': newTodo.id,'description': newTodo.description,'isDone': newTodo.isDone == true ? 'true' : 'false',},);

For Delete I used -
final db = await _getDatabse();
await db.rawDelete('DELETE FROM todo WHERE id = ?',[deleatableTodo.id],);

What shoudl I do to update a todo?

答案1

得分: 1

// 更新一个项目通过id
static Future<int> updateItem(
    int id, String? description, String? isDone) async {
  final db = await SQLHelper.db();

  final data = {
    'description': description,
    'isDone': isDone == true ? 'true' : 'false'
  };

  final result =
      await db.update('items', data, where: "id = ?", whereArgs: [id]);
  return result;
}
// 更新一个已存在的日志
Future<void> _updateItem(int id) async {
  await SQLHelper.updateItem(
      id, _titleController.text, _descriptionController.text);
  _refreshJournals(); // 这是一个选择语句
}
// 当你按下按钮时调用它
if (id != null) {
   await _updateItem(id);
}

请注意,我没有翻译视频的内容,因为它是链接,而且视频内容是阿拉伯语。如果您有其他翻译需求,请随时告诉我。

英文:
  // Update an item by id
  static Future&lt;int&gt; updateItem(
      int id, String? descrption, String? isDone) async {
    final db = await SQLHelper.db();

    final data = {
      &#39;description&#39;: descrption,
      &#39;isDone&#39;: : newTodo.isDone == true ? &#39;true&#39; : &#39;false&#39;
    };

    final result =
        await db.update(&#39;items&#39;, data, where: &quot;id = ?&quot;, whereArgs: [id]);
    return result;
  }

Make this function:

  // Update an existing journal
  Future&lt;void&gt; _updateItem(int id) async {
    await SQLHelper.updateItem(
        id, _titleController.text, _descriptionController.text);
    _refreshJournals(); // this is an select statement
  }

Then call it when you press the button:

if (id != null) {
   await _updateItem(id);
}

And here is a video if would like it as gives all operation but its in an Arabic language:

Complete CRUD Operations with SQLite

huangapple
  • 本文由 发表于 2023年6月18日 23:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定