DBHelper.dart
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import '../screens/models/UserBottle.dart';
final String tableName = 'medicinename';
class DBHelper {
DBHelper._();
static final DBHelper _db = DBHelper._();
factory DBHelper() => _db;
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await initDB();
return _database;
}
initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, 'medicinename.db');
return await openDatabase(
path,
version: 1,
onCreate: (Database db, int version) async {
await db.execute('''
CREATE TABLE $tableName
(bottleId INTEGER PRIMARY KEY,
bottleName TEXT)
''');
},
);
}
createData(UserBottle bottle) async {
final db = await database;
var res = await db.insert(tableName, bottle.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace);
return res;
}
getBottle(int bottleId) async {
final db = await database;
var res =
await db.query(tableName, where: 'bottleId=?', whereArgs: [bottleId]);
return res.isNotEmpty ? UserBottle.fromJson(res.first) : Null;
}
Future<List<UserBottle>> getAllBottle() async {
final db = await database;
var res = await db.query(tableName);
List<UserBottle> list =
res.isNotEmpty ? res.map((c) => UserBottle.fromJson(c)).toList() : [];
return list;
}
updateBottle(UserBottle bottle) async {
final db = await database;
var res = db.update(tableName, bottle.toJson(),
where: 'bottleId=?', whereArgs: [bottle.bottleId]);
return res;
}
}