Class Database
- Direct Known Subclasses:
ThreadSafeDatabase
Allows access to SQLite specifically connecting to a database and executing sql queries on the data.
There is more thorough coverage of the Database API here.
The Database class abstracts the underlying SQLite of the device if available.
Notice that this might not be supported on all platforms in which case the Database will be null.
SQLite should be used for very large data handling, for small storage
refer to com.codename1.io.Storage which is more portable.
The sample code below presents a Database Explorer tool that allows executing arbitrary SQL and viewing the tabular results:
Toolbar.setGlobalToolbar(true);
Style s = UIManager.getInstance().getComponentStyle("TitleCommand");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_QUERY_BUILDER, s);
Form hi = new Form("SQL Explorer", new BorderLayout());
hi.getToolbar().addCommandToRightBar("", icon, (e) -> {
TextArea query = new TextArea(3, 80);
Command ok = new Command("Execute");
Command cancel = new Command("Cancel");
if(Dialog.show("Query", query, ok, cancel) == ok) {
Database db = null;
Cursor cur = null;
try {
db = Display.getInstance().openOrCreate("MyDB.db");
if(query.getText().startsWith("select")) {
cur = db.executeQuery(query.getText());
int columns = cur.getColumnCount();
hi.removeAll();
if(columns > 0) {
boolean next = cur.next();
if(next) {
ArrayList data = new ArrayList<>();
String[] columnNames = new String[columns];
for(int iter = 0 ; iter
@author Chen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidStarts a transactionabstract voidclose()Closes the databaseabstract voidCommits current transactionstatic voidDeletes databaseabstract voidExecute an update query.voidExecute an update query with params.abstract voidExecute an update query with params.abstract CursorexecuteQuery(String sql) This method should be called with SELECT type statements that return row set.executeQuery(String sql, Object... params) This method should be called with SELECT type statements that return row set it accepts object with params.abstract CursorexecuteQuery(String sql, String[] params) This method should be called with SELECT type statements that return row set.static booleanIndicates weather a database existsstatic StringgetDatabasePath(String databaseName) Returns the file path of the Database if exists and if supported on the platform.static booleanChecks if this platform supports custom database paths.static DatabaseopenOrCreate(String databaseName) Opens a database or create one if not exists.abstract voidRolls back current transactionstatic booleansupportsWasNull(Row row) Checks to see if the given row supports#wasNull(com.codename1.db.Row).static booleanChecks if the last value accessed from a given row was null.
-
Constructor Details
-
Database
public Database()
-
-
Method Details
-
isCustomPathSupported
public static boolean isCustomPathSupported()Checks if this platform supports custom database paths. On platforms that support this, you can pass a file path to
#openOrCreate(java.lang.String),#exists(java.lang.String),#delete(java.lang.String), and#getDatabasePath(java.lang.String).Returns
True on platorms that support custom database paths.
-
openOrCreate
Opens a database or create one if not exists.
Parameters
databaseName: @param databaseName the name of the database. Platforms that support custom database paths (i.e.#isCustomPathSupported()return true), will also accept a file path here.
Returns
Database Object or null if not supported on the platform
Throws
IOException: if database cannot be created
- Throws:
IOException
-
exists
Indicates weather a database exists
NOTE: Not supported in the Javascript port. Will always return false.
Parameters
databaseName: @param databaseName the name of the database. Platforms that support custom database paths (i.e.#isCustomPathSupported()return true), will also accept a file path here.
Returns
true if database exists
-
delete
Deletes database
NOTE: This method is not supported in the Javascript port. Will silently fail.
Parameters
databaseName: @param databaseName the name of the database. Platforms that support custom database paths (i.e.#isCustomPathSupported()return true), will also accept a file path here.
Throws
IOException: if database cannot be deleted
- Throws:
IOException
-
getDatabasePath
Returns the file path of the Database if exists and if supported on the platform.
Parameters
databaseName: @param databaseName The name of the database. Platforms that support custom database paths (i.e.#isCustomPathSupported()return true), will also accept a file path here.
NOTE: This method will return null in the Javascript port.
Returns
the file path of the database
-
wasNull
Checks if the last value accessed from a given row was null. Not all platforms support wasNull(). If the platform does not support it, this will just return false.
Check
#supportsWasNull(com.codename1.db.Row)to see if the platform supports wasNull().Currently wasNull() is supported on UWP, iOS, Android, and JavaSE (Simulator).
Parameters
row: The row to check.
Returns
True if the last value accessed was null.
Throws
IOException
Since
7.0
See also
-
RowExt#wasNull()
-
#supportsWasNull(com.codename1.db.Row)
- Throws:
IOException
-
supportsWasNull
Checks to see if the given row supports
#wasNull(com.codename1.db.Row).Parameters
row: The row to check.
Returns
True if the row supports wasNull().
Throws
IOException
Since
7.0
See also
-
#wasNull(com.codename1.db.Row)
-
RowExt#wasNull()
- Throws:
IOException
-
beginTransaction
Starts a transaction
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
Throws
IOException: if database is not opened
- Throws:
IOException
-
commitTransaction
Commits current transaction
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
Throws
IOException: if database is not opened or transaction was not started
- Throws:
IOException
-
rollbackTransaction
Rolls back current transaction
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
Throws
IOException: if database is not opened or transaction was not started
- Throws:
IOException
-
close
Closes the database
Throws
IOException
- Throws:
IOException
-
execute
Execute an update query. Used for INSERT, UPDATE, DELETE and similar sql statements.
Parameters
sql: the sql to execute
Throws
IOException
- Throws:
IOException
-
execute
Execute an update query with params. Used for INSERT, UPDATE, DELETE and similar sql statements. The sql can be constructed with '?' and the params will be binded to the query
Parameters
-
sql: the sql to execute -
params: to bind to the query where the '?' exists
Throws
IOException
- Throws:
IOException
-
-
execute
Execute an update query with params. Used for INSERT, UPDATE, DELETE and similar sql statements. The sql can be constructed with '?' and the params will be binded to the query
Parameters
-
sql: the sql to execute -
params: @param params to bind to the query where the '?' exists, supported object types are String, byte[], Double, Long and null
Throws
IOException
- Throws:
IOException
-
-
executeQuery
This method should be called with SELECT type statements that return row set.
Parameters
-
sql: the sql to execute -
params: to bind to the query where the '?' exists
Returns
a cursor to iterate over the results
Throws
IOException
- Throws:
IOException
-
-
executeQuery
This method should be called with SELECT type statements that return row set it accepts object with params.
Parameters
-
sql: the sql to execute -
params: @param params to bind to the query where the '?' exists, supported object types are String, byte[], Double, Long and null
Returns
a cursor to iterate over the results
Throws
IOException
- Throws:
IOException
-
-
executeQuery
This method should be called with SELECT type statements that return row set.
Parameters
sql: the sql to execute
Returns
a cursor to iterate over the results
Throws
IOException
- Throws:
IOException
-