> For the complete documentation index, see [llms.txt](https://dbflow.gitbook.io/dbflow/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dbflow.gitbook.io/dbflow/usage2.md).

# Usage Docs

DBFlow is a Kotlin SQLite library for Android that makes it ridiculously easy to interact and use databases. Built with Annotation Processing, code use within a DB is fast, efficient, and type-safe. It removes the tedious (and tough-to-maintain) database interaction code, while providing a very SQLite-like query syntax.

Creating a database is as easy as a few lines of code:

```kotlin
@Database(version = 1)
abstract class AppDatabase: DBFlowDatabase
```

The `@Database` annotation generates a `DatabaseDefinition` which now references your SQLite Database on disk in the file named "AppDatabase.db". You can reference it in code as:

```java
val db = database<AppDatabase>();

// or
database<AppDatabase> { db ->

}
```

To ensure generated code in DBFlow is found by the library, initialize the library in your `Application` class:

```kotlin
class MyApp : Application {

  override fun onCreate() {
    super.onCreate()
    FlowManager.init(this)
  }
}
```

By default, DBFlow generates the `GeneratedDatabaseHolder` class, which is instantiated once by reflection, only once in memory.

Creating a table is also very simple:

```kotlin
@Table(database = AppDatabase::class, name = "User2")
class User(@PrimaryKey var id: Int = 0,
           var firstName: String? = null,
           var lastName: String? = null,
           var email: String? = null)
```

Then to create, read, update, and delete the model:

```kotlin
// always utilize DB transactions when possible.
databaseForTable<User>().executeTransaction { db ->
  val user = User(id = UUID.randomUUID(),
                name = "Andrew Grosner",
                age = 27)
  user.insert(db)

  user.name = "Not Andrew Grosner";
  user.update(db)

  user.delete(db)
}

// find adult users synchronously
val users = (select from User::class
               where (User_Table.age greaterThan 18))
            .queryList(database<AppDatabase>())

// or asynchronous retrieval (preferred)
(select from User::class where User_Table.age.greaterThan(18))
 .async(database<AppDatabase>()) { queryList(it) }
 .execute(
  success = { transaction, result ->
   // use result here
  },
  error = { transaction, error ->
  // handle any errors
  }
 )

// use coroutines! or RX3 or LiveData
async {
   val result = (delete<SimpleModel>() where SimpleModel_Table.name.eq("5"))
    .awaitTransact(db) { executeUpdateDelete(database) }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dbflow.gitbook.io/dbflow/usage2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
