Skip to content

Commit eaa472a

Browse files
committed
Implement SQLite helper
1 parent fab0b2e commit eaa472a

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

lib/sqlite_helper.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'package:sqflite/sqflite.dart';
2+
3+
final String tableName='items';
4+
final String colId='id';
5+
final String colTitle='title';
6+
final String colStatus='status';
7+
8+
class SqliteHelper {
9+
late Database _database;
10+
11+
open() async {
12+
_database = await openDatabase(
13+
'local_db.db',
14+
version: 1,
15+
onCreate: (Database db,int version) async{
16+
await db.execute('Create table $tableName ('
17+
'$colId integer primary key autoincrement,'
18+
'$colTitle text not null,'
19+
'$colStatus integer not null'
20+
')');
21+
}
22+
);
23+
}
24+
25+
Future<bool> insert(Map<String,dynamic> dataToInsert) async{
26+
await open();
27+
int rowId=await _database.insert(tableName, dataToInsert);
28+
return rowId>0;
29+
}
30+
31+
Future<List<Map>> fetchAll() async {
32+
await open();
33+
List<Map> items=await _database.query(tableName);
34+
return items;
35+
}
36+
37+
Future<List<Map>> fetchAllOrderedByID() async {
38+
await open();
39+
List<Map> items=await _database.query(tableName,orderBy: 'id desc');
40+
return items;
41+
}
42+
43+
Future<Map> fetchOne(int id) async {
44+
await open();
45+
List<Map> items=await _database.query(tableName,where: '$colId=?',whereArgs: [id]);
46+
return items.first;
47+
}
48+
49+
Future<bool> update(int id,Map<String,dynamic> dataToUpdate) async {
50+
await open();
51+
int rowsUpdated=await _database.update(tableName, dataToUpdate,where: '$colId=?',whereArgs: [id]);
52+
return rowsUpdated>0;
53+
}
54+
55+
Future<bool> delete(int id) async {
56+
await open();
57+
int rowsDeleted=await _database.delete(tableName,where: '$colId=?',whereArgs: [id]);
58+
return rowsDeleted==1;
59+
}
60+
61+
Future<bool> deleteAll()async{
62+
await open();
63+
int rowsDeleted=await _database.delete(tableName);
64+
return rowsDeleted>0;
65+
}
66+
}

pubspec.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ packages:
121121
url: "https://pub.dartlang.org"
122122
source: hosted
123123
version: "1.8.2"
124+
sqflite:
125+
dependency: "direct main"
126+
description:
127+
name: sqflite
128+
url: "https://pub.dartlang.org"
129+
source: hosted
130+
version: "2.0.3+1"
131+
sqflite_common:
132+
dependency: transitive
133+
description:
134+
name: sqflite_common
135+
url: "https://pub.dartlang.org"
136+
source: hosted
137+
version: "2.3.0"
124138
stack_trace:
125139
dependency: transitive
126140
description:
@@ -142,6 +156,13 @@ packages:
142156
url: "https://pub.dartlang.org"
143157
source: hosted
144158
version: "1.1.0"
159+
synchronized:
160+
dependency: transitive
161+
description:
162+
name: synchronized
163+
url: "https://pub.dartlang.org"
164+
source: hosted
165+
version: "3.0.0+3"
145166
term_glyph:
146167
dependency: transitive
147168
description:
@@ -165,3 +186,4 @@ packages:
165186
version: "2.1.2"
166187
sdks:
167188
dart: ">=2.17.1 <3.0.0"
189+
flutter: ">=3.0.0"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies:
3434
# The following adds the Cupertino Icons font to your application.
3535
# Use with the CupertinoIcons class for iOS style icons.
3636
cupertino_icons: ^1.0.2
37+
sqflite: ^2.0.3+1
3738

3839
dev_dependencies:
3940
flutter_test:

0 commit comments

Comments
 (0)