Search code examples
flutterdartsqflite

Problem with editing data with sqflite in flutter


What is wrong in this code? In debug console shown write sql code, but for some reason it doesn't work

  Future<void> _toggleTodoItem(TodoItem todo) async {
    final int count = await this._db.rawUpdate(
        /*sql=*/ '''
      UPDATE $kDbTableName
      SET content = ${todo.content},
      SET number = ${todo.number}
      WHERE id = ${todo.id};''');
    print('Updated $count records in db.');
  }

There is an error

E/SQLiteLog( 7167): (1) near "SET": syntax error in "UPDATE example1_tbl
E/SQLiteLog( 7167):       SET content = n,
E/SQLiteLog( 7167):       SET number = 1
E/SQLiteLog( 7167):       WHERE id = 7;"
E/flutter ( 7167): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "SET": syntax error (code 1 SQLITE_ERROR): , while compiling: UPDATE example1_tbl
E/flutter ( 7167):       SET content = n,
E/flutter ( 7167):       SET number = 1
E/flutter ( 7167):       WHERE id = 7;) sql '      UPDATE example1_tbl
E/flutter ( 7167):       SET content = n,
E/flutter ( 7167):       SET number = 1
E/flutter ( 7167):       WHERE id = 7;' args []}

Solution

  • "UPDATE" syntax doesn't look like that (https://sqlite.org/lang_update.html). You want:

    UPDATE example1_tbl SET content = 'n', number = 1 WHERE id = 7
    

    And you should also be using parameters (https://github.com/tekartik/sqflite/blob/master/sqflite/doc/sql.md#parameters). Don't use text inserted into .rawUpdate unless you want to be subject to Bobby Tables attacks (https://bobby-tables.com).