text stringlengths 1 474 |
|---|
// constructed for each platform. |
join(await getDatabasesPath(), 'doggie_database.db'), |
// When the database is first created, create a table to store dogs. |
onCreate: (db, version) { |
// Run the CREATE TABLE statement on the database. |
return db.execute( |
'CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)', |
); |
}, |
// Set the version. This executes the onCreate function and provides a |
// path to perform database upgrades and downgrades. |
version: 1, |
);<code_end> |
<topic_end> |
<topic_start> |
5. Insert a Dog into the database |
Now that you have a database with a table suitable for storing information |
about various dogs, it’s time to read and write data.First, insert a Dog into the dogs table. This involves two steps: |
<code_start>class Dog { |
final int id; |
final String name; |
final int age; |
Dog({ |
required this.id, |
required this.name, |
required this.age, |
}); |
// Convert a Dog into a Map. The keys must correspond to the names of the |
// columns in the database. |
Map<String, Object?> toMap() { |
return { |
'id': id, |
'name': name, |
'age': age, |
}; |
} |
// Implement toString to make it easier to see information about |
// each dog when using the print statement. |
@override |
String toString() { |
return 'Dog{id: $id, name: $name, age: $age}'; |
} |
}<code_end> |
<code_start>// Define a function that inserts dogs into the database |
Future<void> insertDog(Dog dog) async { |
// Get a reference to the database. |
final db = await database; |
// Insert the Dog into the correct table. You might also specify the |
// `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', |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.