public abstract class

Database

extends Object
java.lang.Object
   ↳ com.neomades.database.Database

Class Overview

Exposes methods to manage a SQL database.

Database has methods to create, delete, execute SQL commands, and perform other common database management tasks.

Database names must be unique within an application, but not necessarily across all applications.

See Also

Summary

Protected Constructors
Database()
Default constructor.
Public Methods
abstract void beginTransaction()
Begins a transaction in EXCLUSIVE mode.
abstract void close()
Releases a reference to the object, closing the object if the last reference was released.
abstract void commitTransaction()
Commits a transaction.
static String[] databaseList()
Returns an array of strings naming the private databases of the application.
static void deleteDatabase(String databaseName)
Deletes an existing private Database.
abstract Cursor execQuery(String sql)
Runs the provided SQL and returns a Cursor over the result set.
abstract Cursor execQuery(String sql, String[] params)
Runs the provided SQL and returns a Cursor over the result set.
abstract void execSQL(String sql, String[] params)
Executes a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
abstract void execSQL(String sql)
Executes a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
abstract void execSQL(String sql, Object[] params)
Executes a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
static boolean exists(String databaseName)
Checks if a Database exists.
static File getDatabasePath(String databaseName)
Returns the absolute path on the file system where the database with the given name is stored.
abstract String getName()
Returns the unique name (in the application) of the database.
abstract int getVersion()
Returns the version of the Database.
static Database openOrCreateDatabase(String databaseName)
Opens a new private Database.
static Database openOrCreateDatabase(String databaseName, int version, DatabaseOpenHelper helper)
Opens a new private Database using a helper object to create, open, and/or manage a database.
abstract void rollbackTransaction()
Rollbacks (cancels) the current transaction.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected Database ()

Default constructor.

Public Methods

public abstract void beginTransaction ()

Begins a transaction in EXCLUSIVE mode. It means that no other database connection (except for read_uncommitted connections) will be able to read the database and no other connection without exception will be able to write the database until the transaction is complete

Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being committed (using commitTransaction()).

public abstract void close ()

Releases a reference to the object, closing the object if the last reference was released.

public abstract void commitTransaction ()

Commits a transaction.

public static String[] databaseList ()

Returns an array of strings naming the private databases of the application.

Returns
  • an array of strings naming the private databases

public static void deleteDatabase (String databaseName)

Deletes an existing private Database.

Parameters
databaseName the name (unique) of the database

public abstract Cursor execQuery (String sql)

Runs the provided SQL and returns a Cursor over the result set.

Note: the returned Cursor will be empty if the query is not a SELECT. Moreover in that case, the query will not be immediately run. To execute the request, call moveToFirst(). Even after the query execution, the Cursor will remain empty.

Parameters
sql the SQL query. The SQL string must not be ; terminated
Returns
  • a Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized.

public abstract Cursor execQuery (String sql, String[] params)

Runs the provided SQL and returns a Cursor over the result set.

Note: the returned Cursor will be empty if the query is not a SELECT. Moreover in that case, the query will not be immediately run. To execute the request, call moveToFirst(). Even after the query execution, the Cursor will remain empty.

Parameters
sql the SQL query. The SQL string must not be ; terminated
params You may include ? in where clause in the query, which will be replaced by the values from params. The values will be bound as Strings.
Returns
  • a Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized.

public abstract void execSQL (String sql, String[] params)

Executes a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE. For INSERT, UPDATE and DELETE statements, use execSQL(String) instead.

For example, the following are good candidates for using this method:

  • ALTER TABLE
  • CREATE or DROP table / trigger / view / index / virtual table
  • REINDEX
  • RELEASE
  • SAVEPOINT
  • PRAGMA that returns no data

Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported
params only String is supported

public abstract void execSQL (String sql)

Executes a single SQL statement that is NOT a SELECT or any other SQL statement that returns data. It has no means to return any data (such as the number of affected rows).

Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.

public abstract void execSQL (String sql, Object[] params)

Executes a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE. For INSERT, UPDATE and DELETE statements, use execSQL(String) instead.

For example, the following are good candidates for using this method:

  • ALTER TABLE
  • CREATE or DROP table / trigger / view / index / virtual table
  • REINDEX
  • RELEASE
  • SAVEPOINT
  • PRAGMA that returns no data

Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported
params only byte[], String, Long and Double are supported

public static boolean exists (String databaseName)

Checks if a Database exists.

Parameters
databaseName the name (unique) of the database
Returns
  • true if the Database exists, false otherwise

public static File getDatabasePath (String databaseName)

Returns the absolute path on the file system where the database with the given name is stored.

Parameters
databaseName name of the database for which you would like to get the path.
Returns
  • an absolute path to the given database.

public abstract String getName ()

Returns the unique name (in the application) of the database.

Returns
  • the unique name (in the application) of the database

public abstract int getVersion ()

Returns the version of the Database.

Returns
  • the version of the Database

public static Database openOrCreateDatabase (String databaseName)

Opens a new private Database. Create the database file if it doesn't exist.

Parameters
databaseName the name (unique) of the database
Returns
  • the contents of a newly created database with the given name

public static Database openOrCreateDatabase (String databaseName, int version, DatabaseOpenHelper helper)

Opens a new private Database using a helper object to create, open, and/or manage a database. Create the database file if it doesn't exist.

Parameters
databaseName the name (unique) of the database
version the version of the database
helper a helper object to create, open, and/or manage a database
Returns
  • the contents of a newly created database with the given name

public abstract void rollbackTransaction ()

Rollbacks (cancels) the current transaction.