· 7 years ago · Jan 08, 2019, 06:38 AM
1import 'dart:io';
2
3import 'package:path/path.dart';
4import 'package:sqflite/sqflite.dart';
5import 'package:path_provider/path_provider.dart';
6
7class DatabaseHelper {
8
9 static final _databaseName = "MyDatabase.db";
10 static final _databaseVersion = 1;
11
12 static final table = 'my_table';
13
14 static final columnId = '_id';
15 static final columnName = 'name';
16 static final columnAge = 'age';
17
18 // make this a singleton class
19 DatabaseHelper._privateConstructor();
20 static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
21
22 // only have a single app-wide reference to the database
23 static Database _database;
24 Future<Database> get database async {
25 if (_database != null) return _database;
26 // lazily instantiate the db the first time it is accessed
27 _database = await _initDatabase();
28 return _database;
29 }
30
31 // this opens the database (and creates it if it doesn't exist)
32 _initDatabase() async {
33 Directory documentsDirectory = await getApplicationDocumentsDirectory();
34 String path = join(documentsDirectory.path, _databaseName);
35 return await openDatabase(path,
36 version: _databaseVersion,
37 onCreate: _onCreate);
38 }
39
40 // SQL code to create the database table
41 Future _onCreate(Database db, int version) async {
42 await db.execute('''
43 CREATE TABLE $table (
44 $columnId INTEGER PRIMARY KEY,
45 $columnName TEXT NOT NULL,
46 $columnAge INTEGER NOT NULL
47 )
48 ''');
49 }
50
51 // Helper methods
52
53 // Inserts a row in the database where each key in the Map is a column name
54 // and the value is the column value. The return value is the id of the
55 // inserted row.
56 Future<int> insert(Map<String, dynamic> row) async {
57 Database db = await instance.database;
58 return await db.insert(table, row);
59 }
60
61 // All of the rows are returned as a list of maps, where each map is
62 // a key-value list of columns.
63 Future<List<Map<String, dynamic>>> queryAllRows() async {
64 Database db = await instance.database;
65 return await db.query(table);
66 }
67
68 // All of the methods (insert, query, update, delete) can also be done using
69 // raw SQL commands. This method uses a raw query to give the row count.
70 Future<int> queryRowCount() async {
71 Database db = await instance.database;
72 return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
73 }
74
75 // We are assuming here that the id column in the map is set. The other
76 // column values will be used to update the row.
77 Future<int> update(Map<String, dynamic> row) async {
78 Database db = await instance.database;
79 int id = row[columnId];
80 return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
81 }
82
83 // Deletes the row specified by the id. The number of affected rows is
84 // returned. This should be 1 as long as the row exists.
85 Future<int> delete(int id) async {
86 Database db = await instance.database;
87 return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
88 }
89}