easy_sqflite_orm

Creator: coderz1093

Last updated:

0 purchases

easy_sqflite_orm Image
easy_sqflite_orm Images

Languages

Categories

Add to Cart

Description:

easy sqflite orm

Here's the updated README with the name changed to "EasySqfliteORM":
EasySqfliteORM #
EasySqfliteORM is a simple and dynamic SQLite ORM (Object-Relational Mapping) solution for Flutter. It helps developers interact with SQLite databases without writing raw SQL. This package supports common database operations such as CRUD, table creation, batch operations, transactions, and more.
Features #

Dynamic Table Creation: Create tables dynamically as needed.
CRUD Operations: Perform standard database operations such as insert, query, update, and delete.
One-to-One, One-to-Many, and Many-to-Many Relationships: Easily set up relationships between tables.
Batch Operations: Perform bulk inserts and batch processing.
Transaction Support: Ensure atomicity with transaction management.
Join Queries and Pagination: Execute join queries and paginate results.
Aggregate Functions: Use functions like SUM, MIN, MAX, and AVG.
Index Creation: Optimize query performance by creating indexes.

Getting Started #
1. Add Dependency #
First, add the sqflite and path packages to your pubspec.yaml:
dependencies:
sqflite: ^2.0.0+3
path: ^1.8.1
copied to clipboard
2. Initialize the ORM #
Before performing any database operations, you need to initialize EasySqfliteORM with the database name:
import 'package:easy_sqflite_orm/easy_sqflite_orm.dart';

void main() async {
await EasySqfliteORM.init('my_database.db');
runApp(MyApp());
}
copied to clipboard
3. Create Tables #
You can create tables dynamically by specifying the table name and column definitions:
await EasySqfliteORM().createTable(
'users',
{'id': 'INTEGER PRIMARY KEY', 'name': 'TEXT', 'age': 'INTEGER'},
);
copied to clipboard
4. Insert Data #
To insert data into a table:
await EasySqfliteORM().insert('users', {'name': 'John Doe', 'age': 30});
copied to clipboard
5. Query Data #
You can query data with various options like filtering, limiting, and offsetting:
List<Map<String, dynamic>> users = await EasySqfliteORM().query('users');
copied to clipboard
6. Update Data #
To update existing records:
await EasySqfliteORM().update(
'users',
{'name': 'John Smith'},
'id = ?',
[1],
);
copied to clipboard
7. Delete Data #
To delete records:
await EasySqfliteORM().delete('users', 'id = ?', [1]);
copied to clipboard
Advanced Usage #
One-to-One Relationship #
To create a one-to-one relationship between tables:
await EasySqfliteORM().createOneToOneTable(
'profiles',
'users',
'user_id',
{'id': 'INTEGER PRIMARY KEY', 'bio': 'TEXT'},
);
copied to clipboard
One-to-Many Relationship #
To create a one-to-many relationship:
await EasySqfliteORM().createOneToManyTable(
'posts',
'users',
'user_id',
{'id': 'INTEGER PRIMARY KEY', 'content': 'TEXT'},
);
copied to clipboard
Many-to-Many Relationship #
To create a many-to-many relationship using a junction table:
await EasySqfliteORM().createManyToManyTable(
'user_roles',
'users',
'roles',
'user_id',
'role_id',
);
copied to clipboard
Transactions #
Perform a transaction:
await EasySqfliteORM().transaction((txn) async {
await txn.insert('users', {'name': 'Alice', 'age': 25});
await txn.insert('users', {'name': 'Bob', 'age': 28});
});
copied to clipboard
Batch Operations #
Use batch processing to perform multiple operations at once:
await EasySqfliteORM().performBatch((batch) {
batch.insert('users', {'name': 'Charlie', 'age': 32});
batch.update('users', {'age': 33}, 'name = ?', ['Charlie']);
});
copied to clipboard
Aggregate Functions #
Get the sum of a column:
int? totalAge = await EasySqfliteORM().sum('users', 'age');
copied to clipboard
Pagination #
Retrieve paginated results:
List<Map<String, dynamic>> pageResults = await EasySqfliteORM().paginate('users', 10, 0); // Page 1
copied to clipboard
Join Queries #
Perform an inner join between two tables:
List<Map<String, dynamic>> result = await EasySqfliteORM().joinQuery(
'users',
'profiles',
'users.id = profiles.user_id',
);
copied to clipboard
Index Creation #
Create an index on a table column:
await EasySqfliteORM().createIndex('users', 'name');
copied to clipboard
Example App #
Here is a sample Flutter app that demonstrates how to use EasySqfliteORM:
import 'package:flutter/material.dart';
import 'package:easy_sqflite_orm/easy_sqflite_orm.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasySqfliteORM.init('my_database.db');
await EasySqfliteORM().createTable(
'users',
{'id': 'INTEGER PRIMARY KEY', 'name': 'TEXT', 'age': 'INTEGER'},
);
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'EasySqfliteORM Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: UserListScreen(),
);
}
}

class UserListScreen extends StatefulWidget {
@override
_UserListScreenState createState() => _UserListScreenState();
}

class _UserListScreenState extends State<UserListScreen> {
List<Map<String, dynamic>> _users = [];

@override
void initState() {
super.initState();
_fetchUsers();
}

Future<void> _fetchUsers() async {
List<Map<String, dynamic>> users = await EasySqfliteORM().query('users');
setState(() {
_users = users;
});
}

Future<void> _addUser(String name, int age) async {
await EasySqfliteORM().insert('users', {'name': name, 'age': age});
_fetchUsers();
}

Future<void> _updateUser(int id, String newName, int newAge) async {
await EasySqfliteORM().update(
'users',
{'name': newName, 'age': newAge},
'id = ?',
[id],
);
_fetchUsers();
}

Future<void> _deleteUser(int id) async {
await EasySqfliteORM().delete('users', 'id = ?', [id]);
_fetchUsers();
}

void _showAddUserDialog() {
String name = '';
int age = 0;

showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add User'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
onChanged: (value) => name = value,
decoration: InputDecoration(labelText: 'Name'),
),
TextField(
onChanged: (value) => age = int.tryParse(value) ?? 0,
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: 'Age'),
),
],
),
actions: [
TextButton(
onPressed: () {
_addUser(name, age);
Navigator.of(context).pop();
},
child: Text('Add'),
),
],
);
},
);
}

void _showUpdateUserDialog(int id, String currentName, int currentAge) {
String newName = currentName;
int newAge = currentAge;

showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Update User'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
onChanged: (value) => newName = value,
decoration: InputDecoration(labelText: 'Name'),
controller: TextEditingController(text: currentName),
),
TextField(
onChanged: (value) => newAge = int.tryParse(value) ?? 0,
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: 'Age'),
controller: TextEditingController(text: currentAge.toString()),
),
],
),
actions: [
TextButton(
onPressed: () {
_updateUser(id, newName, newAge);
Navigator.of(context).pop();
},
child: Text('Update'),
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('User List'),
),
body: ListView.builder(
itemCount: _users.length,
itemBuilder: (context, index) {
final user = _users[index];
return ListTile(
title: Text('${user['name']} (Age: ${user['age']})'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(Icons.edit),
onPressed: () =>
_showUpdateUserDialog(user['id'], user['name'], user['age']),
),
IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteUser(user['id']),
),
],
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: _showAddUserDialog,
child: Icon(Icons.add),
),
);
}
}

copied to clipboard
License #
This project is licensed under the MIT License.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.