work in progress database

This commit is contained in:
2023-09-28 13:38:09 +02:00
parent 52d7defb75
commit 64b4f64f8c
3 changed files with 121 additions and 26 deletions

View File

@@ -22,28 +22,26 @@ class NoteDatabase {
} }
Future<Database> _initDB(String filePath) async { Future<Database> _initDB(String filePath) async {
// On Android, it is typically data/data//databases.
// On iOS and MacOS, it is the Documents directory.
final databasePath = await getDatabasesPath(); final databasePath = await getDatabasesPath();
// Directory databasePath = await getApplicationDocumentsDirectory();
final path = join(databasePath, filePath); final path = join(databasePath, filePath);
return await openDatabase(path, version: 1, onCreate: _createDB); return await openDatabase(path, version: 1, onCreate: _createDB);
} }
Future _createDB(Database database, int version) async { Future _createDB(Database database, int version) async {
//check if the database is created if (await isDatabaseExists()) {
if (database.query(noteTable) != null) {
print("Database already created"); print("Database already created");
deleteDatabase();
print("Database deleted");
}
}else{
print("demo data inserting");
await database.execute('''CREATE TABLE promemoria ( await database.execute('''CREATE TABLE promemoria (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL, title TEXT NOT NULL,
creationDate TEXT NOT NULL, creationDate TEXT NOT NULL,
lastModificationDate TEXT, lastModificationDate TEXT,
expirationDate TEXT, expirationDate TEXT,
arrayPromemoria TEXT,
description TEXT, description TEXT,
priority TEXT, priority TEXT,
color TEXT color TEXT
@@ -55,29 +53,24 @@ class NoteDatabase {
title TEXT NOT NULL, title TEXT NOT NULL,
creationDate TEXT NOT NULL, creationDate TEXT NOT NULL,
lastModificationDate TEXT, lastModificationDate TEXT,
arrayPromemoria TEXT,
description TEXT description TEXT
); );
'''); ''');
print("database created"); print("database created");
}
await fillDemoData(database, version); await fillDemoData(database, version);
} }
Future fillDemoData(Database database, int version) async { Future fillDemoData(Database database, int version) async {
print("boh speriamo funzioni");
// Add some fake accounts
// Add fake categories // Add fake categories
await database.execute(''' await database.execute('''
INSERT INTO note ( INSERT INTO note (
title, title,
creationDate, creationDate,
lastModificationDate, lastModificationDate,
arrayPromemoria,
description description
) VALUES ( ) VALUES (
'Nota 2', 'Nota 2',
@@ -93,6 +86,7 @@ class NoteDatabase {
title, title,
creationDate, creationDate,
lastModificationDate, lastModificationDate,
arrayPromemoria,
description description
) VALUES ( ) VALUES (
'Nota 2', 'Nota 2',
@@ -109,6 +103,7 @@ class NoteDatabase {
creationDate, creationDate,
lastModificationDate, lastModificationDate,
expirationDate, expirationDate,
arrayPromemoria,
description, description,
priority, priority,
color color
@@ -130,6 +125,7 @@ class NoteDatabase {
creationDate, creationDate,
lastModificationDate, lastModificationDate,
expirationDate, expirationDate,
arrayPromemoria,
description, description,
priority, priority,
color color
@@ -159,6 +155,50 @@ class NoteDatabase {
} }
} }
Future close() async {
final database = await instance.database;
database.close();
}
Future<void> deleteDatabase() async {
final databasePath = await getDatabasesPath();
final path = join(databasePath, 'note.db');
databaseFactory.deleteDatabase(path);
}
Future<bool> isDatabaseExists() async {
final databasePath = await getDatabasesPath();
final path = join(databasePath, 'note.db');
// Apre il database in modalità sola lettura.
Database database = await openDatabase(path, readOnly: true);
// Restituisce true se il database è stato aperto correttamente, altrimenti false.
return database.isOpen;
}
Future<void> createNote(Database database, Note note) async {
await database.execute('''
INSERT INTO note (
title,
creationDate,
lastModificationDate,
arrayPromemoria,
description,
) VALUES (
'$note.title}',
'$note.creationDate',
'$note.lastModificationDate',
'$note.arrayPromemoria.toString()',
'$note.description',
)
''');
print('note $note.title inserted');
}
Future<List<Map>> selectAllPromemoria() async { Future<List<Map>> selectAllPromemoria() async {
final db = await database; final db = await database;
@@ -167,15 +207,68 @@ class NoteDatabase {
return maps; return maps;
} }
Future close() async {
final database = await instance.database; Future<List<Map>> selectAllNotes() async {
database.close(); final db = await database;
final List<Map<String, dynamic>> maps = await db.query(noteTable);
return maps;
} }
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
Future<void> deleteDatabase() async { Future<void> createPromemoria(Database database,
final databasePath = await getDatabasesPath(); Promemoria promemoria) async {
final path = join(databasePath, 'note.db'); await database.execute('''
databaseFactory.deleteDatabase(path); INSERT INTO promemoria (
title,
creationDate,
lastModificationDate,
expirationDate,
arrayPromemoria,
description,
priority,
color
) VALUES (
'$promemoria.title',
'$promemoria.creationDate',
'$promemoria.lastModificationDate',
'$promemoria.expirationDate',
'$promemoria.arrayPromemoria.toString()',
'$promemoria.description',
'$promemoria.priority',
'$promemoria.color'
)
''');
print('promemoria $promemoria.title inserted');
} }
Future<Map<String, Object?>> readPromemoria(int id) async {
final db = await database;
final results = await db.query('SELECT * FROM note where id=$id');
return results.first;
}
Future<Map<String, Object?>> readNote(int id) async {
final db = await database;
final results= await db.query('SELECT * FROM promemoria where id=$id');
return results.first;
}
// Future<void> updatePromemoria(Promemoria promemoria) async {
// final db = await database;
//await db.update('promemoria', promemoria, where: 'id = ?',
// Pass the Dog's id as a whereArg to prevent SQL injection.
// whereArgs: [dog.id],)
//}
} }

View File

@@ -3,4 +3,5 @@ import 'myApp.dart';
void main() { void main() {
runApp(MyApp()); runApp(MyApp());
print("App started");
} }

View File

@@ -4,6 +4,7 @@ import 'navigation.dart';
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key); const MyApp({ Key? key }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(