Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
closed by

I am using this code snippet as an example. It is very simple data model:

class Dog {

  final int id;

  final String name;

  final int age;

  Dog({this.id, this.name, this.age});

}

 

To update the information, I'm using this function:

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],

  );

}

await updateDog(Dog(id: 0, name: 'Fido', age: 42));

 

It works perfectly. But what I am asking is that how to update only the field age without using name?I want to do something like this

await updateDog(Dog(id: 0, age: 35));

 

and expect as a result "name: Figo, age: 35". But instead it removes Fido in null. So I get this as a result: "name: null, age: 35".

closed

4 Answers

0 votes
by (13k points)
 
Best answer

To update only the "age" field of a Dog object without changing the "name" field, you can modify the `updateDog` function to handle this scenario. Here's an updated version of the function:

Future<void> updateDog(Dog dog) async {

  // Get a reference to the database.

  final db = await database;

  // Prepare the update statement.

  String updateQuery = 'UPDATE dogs SET age = ? WHERE id = ?';

  // Execute the update statement with the provided values.

  await db.rawUpdate(updateQuery, [dog.age, dog.id]);

}

With this updated code, you can use the `updateDog` function to update only the "age" field of a Dog object. For example:

await updateDog(Dog(id: 0, age: 35));

This will update the Dog with the provided "id" to have an "age" of 35 while keeping the "name" field unchanged.

0 votes
by (11.7k points)

This is an example from the documentation: 

// Update Fido's age and save it to the database.

  fido = Dog(

    id: fido.id,

    name: fido.name,

    age: fido.age + 7,

  );

  await updateDog(fido);

So you either approach it with the raw SQL query, or query the Dog with the id, then override the fields, then update.

Your updated code is:

await updateDog(Dog(id: 0, age: 35));

When we call the update line, Dog.toMap() will be called and it looks like you are updating the name to null.

Below code is exactly what you want:

Future<Dog> getDog(int id) async {

    List<Map> result = await database.query(..... whereArgs: [id]);

    if (result.length > 0) {

      return new Dog.fromMap(result.first);

    }

    return null;

 }

// Now in code

fido = await getDog(id);

// Update Fido's age and save it to the database.

fido = Dog(

 id: fido.id,

 name: fido.name,

 age: 35, //<--

);

await updateDog(fido);

If you want to get more insights into SQL, checkout this SQL Course from Intellipaat.

0 votes
by (11.4k points)
To update only the 'age' field of the `Dog` object without affecting the 'name' field, you can modify the `updateDog` function to handle the scenario where the 'name' field is not provided. Here's an updated version of the function:

Future<void> updateDog(Dog dog) async {

  // Get a reference to the database.

  final db = await database;

  // Create an empty map to hold the updates.

  final updatedValues = <String, dynamic>{};

  // Check if the 'age' field is provided and add it to the updates.

  if (dog.age != null) {

    updatedValues['age'] = dog.age;

  }

  // Perform the update only if there are valid updates to apply.

  if (updatedValues.isNotEmpty) {

    await db.update(

      'dogs',

      updatedValues,

      where: "id = ?",

      whereArgs: [dog.id],

    );

  }

}

With this modification, when you call `updateDog` and provide a new 'age' value, it will only update the 'age' field in the database and leave the 'name' field unchanged.

Example usage:

await updateDog(Dog(id: 0, age: 35));

This will update the 'age' field of the dog with ID 0 to 35 while preserving the existing 'name' value in the database.
0 votes
by (7.8k points)
To update only the "age" field without modifying the "name" field in your `updateDog` function, you can modify the code as follows:

Future<void> updateDog(Dog dog) async {

  // Get a reference to the database.

  final db = await database;

  // Prepare the update statement.

  final updateFields = <String, dynamic>{};

  if (dog.age != null) {

    updateFields['age'] = dog.age;

  }

  // Update the given Dog.

  await db.update(

    'dogs',

    updateFields,

    // 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],

  );

}

In this modified code, we first create an empty `updateFields` map. Then, we check if the `age` field in the `dog` object is not null. If it's not null, we add it to the `updateFields` map with the key `'age'`.

By doing this, only the "age" field will be updated, and the "name" field will remain unchanged. So when you call `await updateDog(Dog(id: 0, age: 35))`, it will update the age of the dog with ID 0 to 35 without modifying the name.

Note: Make sure that the "age" field in your `Dog` class is nullable (`int? age`) to allow passing `null` when you don't want to update the age.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 29, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Sep 4, 2019 in SQL by Sammy (47.6k points)

Browse Categories

...