Pluma is a SQLite wrapper for Java and Android, based on iOS FMDB.
There are three main classes in Pluma:
Database
- Represents a single SQLite database. Used for executing SQL statements.ResultSet
- Represents the results of a query executed byDatabase
.Statement
- Represents a single compiled SQL statement. UseDatabase.prepareStatement
.
Almost all operations performed by the Database
and ResultSet
throws a SQLiteException
.
Database database = new Database("/path/db.sqlite");
database.open();
Any sort of SQL statement which is not a SELECT statement qualifies as an update.
database.executeUpdate("CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)");
ResultSet resultSet = database.executeQuery("SELECT id, name FROM people");
while (resultSet.next()) {
int id = resultSet.getInt(0);
String name = resultSet.getString(1);
}
In a loop, the next()
closes the ResultSet
automatically when
the statement is done. Otherwise you should always call
close()
on ResultSet
- List
List<Object> args = Arrays.asList(5, 10);
ResultSet resultSet = database.executeQuery("SELECT name FROM people WHERE id = ? OR id = ?", args);
or
ResultSet resultSet = database.executeQuery("SELECT name FROM people WHERE id = ? OR id = ?", 5, 10);
- Named parameters
Map<String, Object> argsMap = new HashMap<String, Object>();
argsMap.put("personId", 5);
ResultSet resultSet = database.executeQuery("SELECT name FROM people WHERE id = :personId", argsMap);