text stringlengths 1 372 |
|---|
a unique id, the name, and the age of each dog. |
<code_start> |
class dog { |
final int id; |
final string name; |
final int age; |
const dog({ |
required this.id, |
required this.name, |
required this.age, |
}); |
} |
<code_end> |
<topic_end> |
<topic_start> |
3. open the database |
before reading and writing data to the database, open a connection |
to the database. this involves two steps: |
info note |
in order to use the keyword await, the code must be placed |
inside an async function. you should place all the following |
table functions inside void main() async {}. |
<code_start> |
// avoid errors caused by flutter upgrade. |
// importing 'package:flutter/widgets.dart' is required. |
WidgetsFlutterBinding.ensureInitialized(); |
// open the database and store the reference. |
final database = openDatabase( |
// set the path to the database. note: using the `join` function from the |
// `path` package is best practice to ensure the path is correctly |
// constructed for each platform. |
join(await getDatabasesPath(), 'doggie_database.db'), |
); |
<code_end> |
<topic_end> |
<topic_start> |
4. create the dogs table |
next, create a table to store information about various dogs. |
for this example, create a table called dogs that defines the data |
that can be stored. each dog contains an id, name, and age. |
therefore, these are represented as three columns in the dogs table. |
for more information about the available datatypes that can be stored in a |
SQLite database, see the official SQLite datatypes documentation. |
<code_start> |
final database = openDatabase( |
// set the path to the database. note: using the `join` function from the |
// `path` package is best practice to ensure the path is correctly |
// 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.