Sqflite Easy Query
A new Flutter project for easy sqlite queries for beginner who did not know how to write queries.
👉 Step One: Extend with MyDb and Add table name to Model Class
you can compare with sample model lib\services\sqlite\models\UserDbModel.dart
class UserDbModel extends SqfliteEasyDb{
String tableName = 'users';
.....
}
👉 Step Second: Migrations
You need to make table modifications queries as follow
â–ª Example of create table
Note: Primary key or autoincrement only use for one column in a table otheriwse you will get expectation Note2: to create table atleast one column is required using method addColumn()
await UserDbModel()
.addColumn(name: "column_name_one", type: ColumnType.integer, isPrimaryKey: true, isAutoIncrement: true)
.addColumn(name: "column_name_two", type: ColumnType.text, isNUll: true)
.execute();
â–ª Example of drop table
A table can be drop by this method dropTable()
await UserDbModel().dropTable().execute();
â–ª Example of add columns in table
To add column to existing table you need to use method addColumn()
await UserDbModel()
.addColumn(name: "column_name_three", type: ColumnType.text, isNUll: true)
.execute();
â–ª Example of delete column in table
// not supported yet
â–ª Example of rename column in table
renameColumn() method is use to rename column mame in a table
await UserDbModel()
.renameColumn(oldName: "column_name_four", newName: "column_name_four")
.execute();
👉 Step Third: Tables Supported Queries Actions
Here are some sample queries that you can use
â–ª Insert Record
UserDbModel(name: "name", fatherName: "father").insert();
â–ª Update Record
UserDbModel(name: "name", fatherName: "father").where("id", 1).update();
â–ª Delete Record
UserDbModel().delete();
â–ª Get Record
UserDbModel().get();
â–ª Count Record
UserDbModel().count();
â–ª Pluck Record
UserDbModel().pluck('column_name_one');
👉 Step Four: Handle output of instance
You can use get query instance as
await UserDbModel().get().queryResult;
or
UserDbModel().get().queryResult.then((value){
print(value);
});
â–ª Available Methods that can apply on Query
await UserDbModel()
// .joining('INNER JOIN table_name_2 ON table_name.id = table_name_2.id')
// .select(['id','column_name_one']) // select specific column
// .whereBetween('id', ['1','10']) // accept only two values in array
// .orWhereBetween('id', ['1','10']) // accept only two values in array
// .whereIn('id', ['1','2','3'])
// .orWhereIn('column_name_one', '1', operator: Operator.equal)
// .whereNotIn('column_name_one', '1', operator: Operator.equal)
// .where('column_name_one', '1', operator: Operator.equal)
// .orWhere('column_name_one', '1', operator: Operator.equal)
// .whereNull(column_name_one)
// .orderBy('column_name_two', 'DESC')
// .skip(1)
// .limit(2)
// .page(2)
.get();
Regards,
Hassan Mehmood
https://zahidaz.com