Convert Figma logo to code with AI

agrosner logoDBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

4,872
598
4,872
44

Top Related Projects

12,626

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Realm is a mobile database: a replacement for SQLite & ORMs

Android Database - first and fast, lightweight on-device vector database

2,620

Insanely easy way to work with Android Database.

3,139

requery - modern SQL based query & persistence for Java / Kotlin / Android

ORMLite Android functionality used in conjunction with ormlite-core

Quick Overview

DBFlow is a fast, powerful, and lightweight ORM (Object-Relational Mapping) Android database library. It simplifies database operations in Android applications by providing an intuitive API for defining models, querying data, and managing database migrations.

Pros

  • Fast and efficient, with minimal runtime impact
  • Extensive query language support, including complex joins and subqueries
  • Supports multiple databases and custom database wrappers
  • Robust migration system for easy schema updates

Cons

  • Steep learning curve for developers new to ORMs
  • Requires annotation processing, which can increase build times
  • Limited support for complex data types and relationships compared to some other ORMs
  • May be overkill for simple database operations in small projects

Code Examples

  1. Defining a model:
@Table(database = AppDatabase::class)
data class User(
    @PrimaryKey var id: Int = 0,
    @Column var name: String? = null,
    @Column var email: String? = null
)
  1. Inserting data:
val user = User(name = "John Doe", email = "john@example.com")
user.save()
  1. Querying data:
val users = (select from User::class
    where (User_Table.name.like("%John%"))
    and (User_Table.email.isNotNull))
    .queryList()
  1. Updating data:
(update<User>()
    set User_Table.name.eq("Jane Doe")
    where User_Table.id.eq(1))
    .execute()

Getting Started

  1. Add DBFlow to your project's build.gradle:
dependencies {
    def dbflow_version = "5.0.0-alpha1"
    kapt "com.github.agrosner.dbflow:processor:$dbflow_version"
    implementation "com.github.agrosner.dbflow:core:$dbflow_version"
    implementation "com.github.agrosner.dbflow:lib:$dbflow_version"
}
  1. Initialize DBFlow in your Application class:
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        FlowManager.init(this)
    }
}
  1. Define your database and models, then start using DBFlow in your app!

Competitor Comparisons

12,626

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Pros of greenDAO

  • Mature and well-established ORM with a large user base
  • Optimized for performance, especially with large datasets
  • Supports encryption out of the box

Cons of greenDAO

  • Less flexible query building compared to DBFlow
  • Requires more boilerplate code for entity definitions
  • Limited support for complex relationships between entities

Code Comparison

greenDAO entity definition:

@Entity
public class User {
    @Id private Long id;
    private String name;
    private int age;
}

DBFlow entity definition:

@Table(database = AppDatabase.class)
public class User extends BaseModel {
    @PrimaryKey private Long id;
    @Column private String name;
    @Column private int age;
}

Both ORMs offer similar functionality for defining entities, but DBFlow's syntax is more concise and uses a base model class for common operations. greenDAO requires separate generator code to create the DAO classes, while DBFlow generates them automatically.

In terms of querying, DBFlow provides a more fluent API:

List<User> users = SQLite.select()
    .from(User.class)
    .where(User_Table.age.greaterThan(18))
    .queryList();

greenDAO's query syntax is more verbose:

List<User> users = userDao.queryBuilder()
    .where(UserDao.Properties.Age.gt(18))
    .list();

Overall, DBFlow offers more flexibility and a more modern API, while greenDAO focuses on performance and stability.

Realm is a mobile database: a replacement for SQLite & ORMs

Pros of Realm

  • Faster query execution and overall performance
  • Built-in support for reactive programming and real-time data synchronization
  • Simpler API and less boilerplate code required

Cons of Realm

  • Larger APK size due to native libraries
  • Limited flexibility in database schema design
  • Steeper learning curve for developers familiar with SQLite-based ORMs

Code Comparison

Realm:

public class Person extends RealmObject {
    @PrimaryKey
    private String name;
    private int age;
    // Getters and setters
}

Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(r -> {
    Person person = r.createObject(Person.class, "John");
    person.setAge(30);
});

DBFlow:

@Table(database = AppDatabase.class)
public class Person extends BaseModel {
    @PrimaryKey
    String name;
    int age;
    // Getters and setters
}

Person person = new Person();
person.setName("John");
person.setAge(30);
person.save();

Both Realm and DBFlow are popular Android ORM libraries, but they have different approaches to database management. Realm uses its own database engine, while DBFlow is built on top of SQLite. The choice between them depends on specific project requirements and developer preferences.

Android Database - first and fast, lightweight on-device vector database

Pros of ObjectBox

  • Higher performance and lower memory footprint
  • Built-in support for relations and queries
  • Simpler API with less boilerplate code

Cons of ObjectBox

  • Less flexible schema migration options
  • Smaller community and ecosystem compared to DBFlow
  • Limited support for custom SQL queries

Code Comparison

ObjectBox:

@Entity
public class User {
    @Id long id;
    String name;
    int age;
}

Box<User> userBox = store.boxFor(User.class);
userBox.put(new User(0, "John", 30));

DBFlow:

@Table(database = AppDatabase.class)
public class User extends BaseModel {
    @PrimaryKey(autoincrement = true)
    long id;
    @Column String name;
    @Column int age;
}

User user = new User();
user.name = "John";
user.age = 30;
user.save();

ObjectBox offers a more concise syntax for defining entities and performing database operations. DBFlow requires more annotations and explicit method calls for similar functionality.

2,620

Insanely easy way to work with Android Database.

Pros of Sugar

  • Simpler API with less boilerplate code
  • Easier to set up and use for beginners
  • Lightweight and minimal impact on app size

Cons of Sugar

  • Limited query capabilities compared to DBFlow
  • Fewer advanced features and customization options
  • Less active development and community support

Code Comparison

Sugar:

@Table
public class Book extends SugarRecord {
    String title;
    String author;
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

DBFlow:

@Table(database = AppDatabase.class)
public class Book extends BaseModel {
    @PrimaryKey(autoincrement = true)
    long id;
    
    @Column
    String title;
    
    @Column
    String author;
}

Both libraries aim to simplify database operations in Android apps, but they differ in complexity and features. Sugar focuses on simplicity and ease of use, making it suitable for smaller projects or developers new to database management. DBFlow offers more advanced features and customization options, making it better suited for larger, more complex applications that require fine-grained control over database operations.

3,139

requery - modern SQL based query & persistence for Java / Kotlin / Android

Pros of requery

  • Supports multiple database types (SQLite, MySQL, PostgreSQL, Oracle)
  • Offers reactive streams and RxJava integration
  • Provides a more flexible and powerful query API

Cons of requery

  • Steeper learning curve due to more complex API
  • Less active community and fewer resources compared to DBFlow
  • May require more boilerplate code for simple use cases

Code Comparison

requery:

@Entity
public abstract class Person {
    @Key @Generated
    int id;
    
    @Index("name_index")
    String name;
    
    int age;
}

DBFlow:

@Table(database = AppDatabase.class)
public class Person extends BaseModel {
    @PrimaryKey(autoincrement = true)
    int id;
    
    @Column
    @Index("name_index")
    String name;
    
    @Column
    int age;
}

Both libraries offer annotation-based entity definitions, but requery's approach is more concise and doesn't require extending a base class. DBFlow's syntax is more explicit in defining the database and column annotations.

requery provides a more flexible querying system and supports multiple database types, making it suitable for complex applications. However, DBFlow's simpler API and active community make it more approachable for beginners and smaller projects.

ORMLite Android functionality used in conjunction with ormlite-core

Pros of ORMLite

  • Mature and stable library with extensive documentation
  • Supports multiple database types beyond SQLite
  • Lightweight with minimal dependencies

Cons of ORMLite

  • Less performant for complex queries compared to newer alternatives
  • Requires more boilerplate code for database operations
  • Limited support for modern Android features and Kotlin

Code Comparison

ORMLite:

@DatabaseTable(tableName = "accounts")
public class Account {
    @DatabaseField(id = true)
    private String name;
    @DatabaseField
    private String password;
}

DBFlow:

@Table(database = AppDatabase::class)
class Account(
    @PrimaryKey var name: String = "",
    @Column var password: String = ""
)

DBFlow offers a more concise syntax, especially when using Kotlin, while ORMLite requires more verbose annotations and field declarations. DBFlow also provides better integration with modern Android development practices and Kotlin features.

While ORMLite is a reliable choice for simple database operations and cross-platform projects, DBFlow offers better performance, more Android-specific features, and a more modern API. However, ORMLite's maturity and extensive documentation may be advantageous for developers who prioritize stability and comprehensive resources.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

README

Image

JitPack.io Android Weekly Android Arsenal

DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to generate SQLite boilerplate for you and provides a powerful SQLite query language that makes using SQLite a joy.

DBFlow is built from a collection of some of the best features of many database libraries. Don't let an ORM or library get in your way, let the code you write in your applications be the best as possible.

DBFlow Contains:

Kotlin: Built using the language, the library is super-concise, null-safe and efficient.

Annotation Processor: Generates the necessary code that you don't need to write.

Core: Contains the main annotations and misc classes that are shared across all of DBFlow.

DBFlow: The main library artifact used in conjunction with the previous two artifacts.

Coroutines: Adds coroutine support for queries.

RX Java: Enable applications to be reactive by listening to DB changes and ensuring your subscribers are up-to-date.

Paging: Android architecture component paging library support for queries via QueryDataSource.

LiveData: Android architecture LiveData support for queries on table changes.

SQLCipher: Easy database encryption support in this library.

SQLite Query Language: Enabling autocompletion on sqlite queries combined with Kotlin language features means SQLite-like syntax.

Changelog

Changes exist in the releases tab.

Usage Docs

For more detailed usage, check out it out here

Including in your project

Add jitpack.io to your project's repositories:

allProjects {
  repositories {
    google() 
    // required to find the project's artifacts
    // place last
    maven { url "https://www.jitpack.io" }
  }
}

Add artifacts to your project:

  apply plugin: 'kotlin-kapt' // only required for kotlin consumers.

  def dbflow_version = "5.0.0-alpha2"
  // or 10-digit short-hash of a specific commit. (Useful for bugs fixed in develop, but not in a release yet)

  dependencies {

    // Use if Kotlin user.
    kapt "com.github.agrosner.dbflow:processor:${dbflow_version}"

    // Annotation Processor
    // if only using Java, use this. If using Kotlin do NOT use this.
    annotationProcessor "com.github.agrosner.dbflow:processor:${dbflow_version}"


    // core set of libraries
    implementation "com.github.agrosner.dbflow:core:${dbflow_version}"
    implementation "com.github.agrosner.dbflow:lib:${dbflow_version}"

    // sql-cipher database encryption (optional)
    implementation "com.github.agrosner.dbflow:sqlcipher:${dbflow_version}"
    implementation "net.zetetic:android-database-sqlcipher:${sqlcipher_version}@aar"

    // RXJava 2 support
    implementation "com.github.agrosner.dbflow:reactive-streams:${dbflow_version}"

    // Kotlin Coroutines
    implementation "com.github.agrosner.dbflow:coroutines:${dbflow_version}"

    // Android Architecture Components Paging Library Support
    implementation "com.github.agrosner.dbflow:paging:${dbflow_version}"

    // Android Architecture Components LiveData Library Support
    implementation "com.github.agrosner.dbflow:livedata:${dbflow_version}"

    // adds generated content provider annotations + support.
    implementation "com.github.agrosner.dbflow:contentprovider:${dbflow_version}"

  }

Pull Requests

I welcome and encourage all pull requests. Here are some basic rules to follow to ensure timely addition of your request: 1. Match coding style (braces, spacing, etc.) This is best achieved using Reformat Code shortcut, command+option+L on Mac and Ctrl+Alt+L on Windows, with Android Studio defaults. 2. If its a feature, bugfix, or anything please only change code to what you specify. 3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :) 4. Pull requests must be made against develop branch. Any other branch (unless specified by the maintainers) will get rejected. 5. Have fun!

Maintainer

Originally created by Raizlabs, a Rightpoint company

Maintained by agrosner (@agrosner)