Skip to content

About

Flutter SQLite package (easiestdb) — declare tables once, then CRUD without writing SQL, built on sqflite

Topics

Resources

Stars

15 stars

Watchers

1 watching

Forks

Latest commit

 

History

42 Commits

Folders and files

Repository files navigation

easiestdb

easiestdb

The easiest and laziest approach to a SQLite database in Flutter. Declare your tables and columns once, then do CRUD without writing a single SQL query — sqflite underneath, so anything it can do, you can still do.

On pub.dev: easiestdb

pub version pub likes

Installation

check out the pub.dev for updated installation instructions

Basic Usage

Init using Database name, Table names and Column names. After that, you can do all kinds of CRUD ( Create, Read, Update, Delete ) operations.

Add as many tables and columns you want using these simple codes.

Initialize the Database and the Tables

static Future<Database> init({ String dbName = 'demo.db', int version = 1, List<DbTable> tables })
DbTable(String tableName, {List<DbColumn> dbColumns})
DbColumn(String columnName, {String columnDataType = " TEXT "})

Suppose, we're trying to create a database of two tables named People and Test, each containing three columns. So, we'll write:

EasiestDb.init(dbName: "Data", version: 1, dbTables: [
    DbTable("People", dbColumns: [
      DbColumn('Col 1'), // Default data type is TEXT
      DbColumn('Col 2'),
    ]),
    DbTable("Test", dbColumns: [
      DbColumn('Col 3'),
      DbColumn('Col 4', columnDataType: "TEXT UNIQUE"),
    ]),
  ]);

Look carefully, we didn't add any ID primary key column. That's because easiestdb library does it by default for each table.

Also, you may add as many SQL Constrains in columnDataType parameter. By default, its set to TEXT.

Add data

static Future<int> addData(int tableIndex, List<Datum> data)
Datum(int columnIndex, String value)

Add data in the 1st(0) table:

EasiestDb.addData(0, [
    Datum(1, "AAA"),
    Datum(2, "123"),
]).then((id) {
    print("ID: $id");
});

To pass anything other than string in addData function, you can just convert them to String and pass it ( may be by using yourValue.toString() )

Get All data from a table

static Future<List<Map<String, dynamic>>> getAllData(int tableIndex, {bool ascending = true})

Get all data from the 1st(0) table ( all the indexes start from 0 )

EasiestDb.getAllData(0).then((listMap) {
    listMap.forEach((map) {
        print("${map.values.elementAt(0)}"); // Showing the value of the ID column
        print("${map.values.elementAt(1)}"); // Showing the value from another column
    });
});

Get one data

static Future<List<Map<String, dynamic>>> getOneRowData(int tableIndex, int rowId)

Get data from the 1st(0) table and the 7th(7) row ( all the indexes start from 0 but the rowId starts from 1 )

EasiestDb.getOneRowData(0, 7).then((listMap) {
    listMap.forEach((map) {
        print("${map.values.elementAt(0)}"); // Showing the value of the ID column
        print("${map.values.elementAt(1)}"); // Showing the value from another column
    });
});

Search data

static Future<List<Map<String, dynamic>>> getRowsByMatchingColumnData(int tableIndex, int columnIndex, var valueToMatch, {bool ascending = true})

Searching data in 1st(0) table in the 2nd(1) column by a value:

EasiestDb.getRowsByMatchingColumnData(0, 1, 'AAA').then((listMap) {
    listMap.forEach((map) {
        print("${map.values.elementAt(0)}"); // Showing the value of the ID column
        print("${map.values.elementAt(1)}"); // Showing the value from another column
    });
});

Update data in a row

static Future<int> updateOneDataById(int tableIndex, int rowId, List<Datum> data)
Datum(int columnIndex, String value)

Update data in the first table (0), 7th(7) row:

EasiestDb.updateOneDataById(0, 7, [
    Datum(1, "F F F F F F "),
    Datum(2, "G G G G G G "),
]).then((count) {
    print("Count: $count");
});

Delete one row data

static Future<int> deleteOneData(int tableIndex, int rowId)

Delete the 7th(7) row from the 1st(0) table:

EasiestDb.deleteOneData(0, 7).then((val) {
    print("Count: $val");
});

Delete a row if value matches in a column

static Future<int> deleteDataBySearchingInColumn(int tableIndex, Datum datum)
Datum(int columnIndex, String value)

Delete a one/more data from the first table if matches value in the 2nd(1) column:

EasiestDb.deleteDataBySearchingInColumn(0, Datum(
    1, "F F F F F F "
)).then((val) {
    print("Count: $val");
});

Delete all data from a table

static void deleteTable(int tableIndex)

Delete/Drop the 2nd(1) table:

EasiestDb.deleteTable(1);

Delete the all data from the database

static void deleteDatabase()

Delete/Drop the whole database:

EasiestDb.deleteDatabase();

Run custom SQL commands

static Database getDatabaseObject()

And last but not the least, if you're trying to do something but there's no a function/method created for that command, you can always get the database object and run any custom command like below:

EasiestDb.getDatabaseObject().execute(sqlCommand)

If you want that helper implemented in easiestdb itself, see the contributing section below.

License

MIT License — Copyright (c) 2020 Fayaz Bin Salam. See LICENSE for the full text.

Thanks

Lastly, I wanna thank the Flutter team for the amazing framework and tekartik for the sqflite library. And thanks to everyone for using easiestdb and thanks in advance to everyone for contributing...

Also, you might wanna try the Android/Java version of this library from here: https://github.com/p32929/EasiestSqlLibrary

Contributing

Contributions are warmly welcomed and greatly appreciated! Whether it's a bug fix, new feature, or improvement, your input helps make this project better for everyone.

Before submitting a pull request, please:

  1. Create an issue describing the feature or bug fix you'd like to work on
  2. Wait for discussion and approval to ensure alignment with project goals
  3. Fork the repository and create your feature branch
  4. Submit your pull request with a clear description of changes

This approach helps avoid duplicate efforts and ensures smooth collaboration. Thank you for considering contributing!

Share

Sharing this repository with your friends is just one click away from here

facebook twitter tumblr pocket pinterest reddit linkedin whatsapp


Support

If this saved you time, you can buy me a coffee — it keeps these projects maintained and free. Other payment options: https://p32929.github.io/SendMoney2MeV1/

Buy Me A Coffee


💼 Using this at a company?

I do fixed-price delivery work on my own projects. One invoice, one date, no hourly billing:

White-label build — this project rebranded, extended and deployed as yours $6,500 · 3 weeks
Custom app from scratch on my own stack, signed and auto-updating $12,500 · 6 weeks
Production-hardening sprint — 72 hours on this project, for your load and your security review $999
Ongoing capacity — one project-week of my time reserved every month $9,000 / month

Full details → p32929.github.io/hire · Email fayazdevinbox@uberip.com — scoping and quotes are free and I answer within one business day.

About

Flutter SQLite package (easiestdb) — declare tables once, then CRUD without writing SQL, built on sqflite

Topics

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages