text stringlengths 1 372 |
|---|
// `conflictalgorithm` to use in case the same dog is inserted twice. |
// |
// in this case, replace any previous data. |
await db.insert( |
'dogs', |
dog.toMap(), |
conflictAlgorithm: ConflictAlgorithm.replace, |
); |
} |
<code_end> |
<code_start> |
// create a dog and add it to the dogs table |
var fido = dog( |
id: 0, |
name: 'fido', |
age: 35, |
); |
await insertDog(fido); |
<code_end> |
<topic_end> |
<topic_start> |
6. retrieve the list of dogs |
now that a dog is stored in the database, query the database |
for a specific dog or a list of all dogs. this involves two steps: |
<code_start> |
// a method that retrieves all the dogs from the dogs table. |
Future<List<Dog>> dogs() async { |
// get a reference to the database. |
final db = await database; |
// query the table for all the dogs. |
final List<Map<String, object?>> dogMaps = await db.query('dogs'); |
// convert the list of each dog's fields into a list of `dog` objects. |
return [ |
for (final { |
'id': id as int, |
'name': name as string, |
'age': age as int, |
} in dogMaps) |
dog(id: id, name: name, age: age), |
]; |
} |
<code_end> |
<code_start> |
// now, use the method above to retrieve all the dogs. |
print(await dogs()); // prints a list that include fido. |
<code_end> |
<topic_end> |
<topic_start> |
7. update a dog in the database |
after inserting information into the database, |
you might want to update that information at a later time. |
you can do this by using the update() |
method from the sqflite library. |
this involves two steps: |
<code_start> |
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], |
); |
} |
<code_end> |
<code_start> |
// 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); |
// print the updated results. |
print(await dogs()); // prints fido with age 42. |
<code_end> |
warning warning |
always use whereArgs to pass arguments to a where statement. |
this helps safeguard against SQL injection attacks. |
do not use string interpolation, such as where: "id = ${dog.id}"! |
<topic_end> |
<topic_start> |
8. delete a dog from the database |
in addition to inserting and updating information about dogs, |
you can also remove dogs from the database. to delete data, |
use the delete() method from the sqflite library. |
in this section, create a function that takes an id and deletes the dog with |
a matching id from the database. to make this work, you must provide a where |
clause to limit the records being deleted. |
<code_start> |
future<void> deleteDog(int id) async { |
// get a reference to the database. |
final db = await database; |
// remove the dog from the database. |
await db.delete( |
'dogs', |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.