text stringlengths 1 372 |
|---|
// use a `where` clause to delete a specific dog. |
where: 'id = ?', |
// pass the dog's id as a whereArg to prevent SQL injection. |
whereArgs: [id], |
); |
} |
<code_end> |
<topic_end> |
<topic_start> |
example |
to run the example: |
<code_start> |
import 'dart:async'; |
import 'package:flutter/widgets.dart'; |
import 'package:path/path.dart'; |
import 'package:sqflite/sqflite.dart'; |
void main() async { |
// 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'), |
// 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, |
); |
// 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, |
); |
} |
// 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), |
]; |
} |
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], |
); |
} |
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', |
// use a `where` clause to delete a specific dog. |
where: 'id = ?', |
// pass the dog's id as a whereArg to prevent SQL injection. |
whereArgs: [id], |
); |
} |
// create a dog and add it to the dogs table |
var fido = dog( |
id: 0, |
name: 'fido', |
age: 35, |
); |
await insertDog(fido); |
// now, use the method above to retrieve all the dogs. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.