SQLite Query Language
DBFlow's SQLite wrapper language attempts to make it as easy as possible to write queries, execute statements, and more.
We will attempt to make this doc comprehensive, but reference the SQLite language for how to formulate queries, as DBFlow follows it as much as possible.
SELECT
The way to query data, SELECT
are started by:
Projections
By default if no parameters are specified in the select()
query, we use the *
wildcard qualifier, meaning all columns are returned in the results.
To specify individual columns, you must use Property
variables. These get generated when you annotate your Model
with columns, or created manually.
To specify methods such as COUNT()
or SUM()
(static import on Method
):
Translates to:
There are more handy methods in Method
.
Operators
DBFlow supports many kinds of operations. They are formulated into a OperatorGroup
, which represent a set of SQLOperator
subclasses combined into a SQLite conditional piece. Property
translate themselves into SQLOperator
via their conditional methods such as eq()
, lessThan()
, greaterThan()
, between()
, in()
, etc.
They make it very easy to construct concise and meaningful queries:
Translates to:
DBFlow supports IN
/NOT IN
and BETWEEN
as well.
A more comprehensive list of operations DBFlow supports and what they translate to:
is(), eq() -> =
isNot(), notEq() -> !=
isNull() -> IS NULL / isNotNull() -> IS NOT NULL
like(), glob()
greaterThan(), greaterThanOrEqual(), lessThan(), lessThanOrEqual()
between() -> BETWEEN
in(), notIn()
Nested Conditions
To create nested conditions (in parenthesis more often than not), just include an OperatorGroup
as a SQLOperator
in a query:
Translates to:
Nested Queries
To create a nested query simply include a query as a Property
via (query).property
:
This appends a WHERE (SELECT * FROM {table} )
to the query.
JOINS
For reference, (JOIN examples).
JOIN
statements are great for combining many-to-many relationships. If your query returns non-table fields and cannot map to an existing object, see about query models
For example we have a table named Customer
and another named Reservations
.
The IProperty.withTable()
method will prepend a NameAlias
or the Table
alias to the IProperty
in the query, convenient for JOIN queries:
in DBFlow:
Order By
Group By
HAVING
LIMIT + OFFSET
UPDATE
DBFlow supports two kind of UPDATE:
1. ModelAdapter
updates
2. Query language updates (for less targeted updates)
For simple UPDATE
for a single or few, concrete set of Model
stick with (1). For powerful multiple Model
update that can span many rows, use (2). In this section we speak on (2). Note: if using model caching, you'll need to clear it out post an operation from (2).
Using DBFlow:
The Set
part of the Update
supports different kinds of values: 1. ContentValues
-> converts to key/value as a SQLOperator
of is()
/eq()
2. SQLOperator
, which are grouped together as part of the SET
statement.
DELETE
DELETE
queries in DBFlow are similiar to Update
in that we have two kinds:
ModelAdapter
deletes.Query language deletes.
For simple DELETE
for a single or few, concrete set of Model
stick with (1). For powerful multiple Model
deletion that can span many rows, use (2). In this section we speak on (2). Note: if using model caching, you'll need to clear it out post an operation from (2).
INSERT
INSERT
queries in DBFlow are also similiar to Update
and Delete
in that we have two kinds:
Model.insert()
SQLite.insert()
For simple INSERT
for a single or few, concrete set of Model
stick with (1). For powerful multiple Model
insertion that can span many rows, use (2). In this section we speak on (2). Note: using model caching, you'll need to clear it out post an operation from (2).
INSERT
supports inserting multiple rows as well.
Trigger
Triggers enable SQLite-level listener operations that perform some operation, modification, or action to run when a specific database event occurs. See for more documentation on its usage.
Case
The SQLite CASE
operator is very useful to evaluate a set of conditions and "map" them to a certain value that returns in a SELECT query.
We have two kinds of case: 1. Simple 2. Searched
The simple CASE query in DBFlow:
The CASE is returned as CustomerGroup
with the valyes of "Domestic" if the country is from the 'USA' otherwise we mark the value as "Foreign". These appear alongside the results set from the SELECT.
The search CASE is a little more complicated in that each when()
statement represents a SQLOperator
, which return a boolean
expression:
Last updated