base_project
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
|
||||
// OLD: import 'databasehelper.dart'; // wrong case fails on Linux Docker - by Azmat
|
||||
import 'DatabaseHelper.dart'; // fixed case capital D+H for Linux build - by Azmat
|
||||
|
||||
class UniController {
|
||||
final conn = DatabaseHelper.instance;
|
||||
|
||||
// get entity
|
||||
Future<List<Map<String, dynamic>>> getentities(String tableName) async {
|
||||
Database db = await conn.database;
|
||||
return await db.query(tableName);
|
||||
}
|
||||
|
||||
// insert
|
||||
Future<int> insertEntity(
|
||||
Map<String, dynamic> entity, String tableName) async {
|
||||
Database db = await conn.database;
|
||||
|
||||
// entity['active'] = entity['active'] ? 1 : 0;
|
||||
|
||||
// Insert data
|
||||
var entityId = await db.insert(tableName, entity);
|
||||
if (entityId != null && entityId > 0) {
|
||||
print('$tableName Id data is inserted. $tableName Id ID: $entityId');
|
||||
} else {
|
||||
print('Failed to insert $tableName.');
|
||||
}
|
||||
|
||||
return entityId;
|
||||
}
|
||||
|
||||
// insert multiple table
|
||||
Future<void> insertEntities(
|
||||
List<Map<String, dynamic>> entities, String tableName) async {
|
||||
Database db = await conn.database;
|
||||
Batch batch = db.batch();
|
||||
|
||||
print('multiple entity is com is $entities');
|
||||
|
||||
for (var entity in entities) {
|
||||
insertEntity(entity, tableName);
|
||||
}
|
||||
|
||||
await batch.commit();
|
||||
}
|
||||
|
||||
// update Entity
|
||||
Future<int> update(String tableName, int entityId,
|
||||
Map<String, dynamic> updatedEntity) async {
|
||||
Database db = await conn.database;
|
||||
|
||||
int rowsAffected = await db.update(tableName, updatedEntity,
|
||||
where: 'id = ?', whereArgs: [entityId]);
|
||||
|
||||
if (rowsAffected > 0) {
|
||||
print('$tableName data updated successfully.');
|
||||
} else {
|
||||
print('Failed to update $tableName data.');
|
||||
}
|
||||
|
||||
return rowsAffected;
|
||||
}
|
||||
|
||||
// Deleting entity
|
||||
|
||||
Future<int> delete(String tableName, int entityId) async {
|
||||
Database db = await conn.database;
|
||||
|
||||
// Delete the company
|
||||
int rowsAffected =
|
||||
await db.delete(tableName, where: 'id = ?', whereArgs: [entityId]);
|
||||
|
||||
if (rowsAffected > 0) {
|
||||
print('$tableName data deleted successfully.');
|
||||
} else {
|
||||
print('Failed to delete comp $tableName any data.');
|
||||
}
|
||||
|
||||
return rowsAffected;
|
||||
}
|
||||
|
||||
// get count
|
||||
Future<int> getDataCount(String tableNmae) async {
|
||||
Database db = await conn.database;
|
||||
|
||||
// Get the count of rows in the company table
|
||||
int count = Sqflite.firstIntValue(
|
||||
await db.rawQuery('SELECT COUNT(*) FROM $tableNmae'))!
|
||||
.toInt();
|
||||
|
||||
print('Number of $tableNmae in the table: $count');
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user