public abstract class Database extends Object
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 Storage
which is more portable.
The sample code below presents a Database Explorer tool that allows executing arbitrary SQL and viewing the tabular results:
Constructor and Description |
---|
Database() |
Modifier and Type | Method and Description |
---|---|
abstract void |
beginTransaction()
Starts a transaction
|
abstract void |
close()
Closes the database
|
abstract void |
commitTransaction()
Commits current transaction
|
static void |
delete(String databaseName)
Deletes database
|
abstract void |
execute(String sql)
Execute an update query.
|
void |
execute(String sql,
Object... params)
Execute an update query with params.
|
abstract void |
execute(String sql,
String[] params)
Execute an update query with params.
|
abstract Cursor |
executeQuery(String sql)
This method should be called with SELECT type statements that return
row set.
|
Cursor |
executeQuery(String sql,
Object... params)
This method should be called with SELECT type statements that return
row set it accepts object with params.
|
abstract Cursor |
executeQuery(String sql,
String[] params)
This method should be called with SELECT type statements that return
row set.
|
static boolean |
exists(String databaseName)
Indicates weather a database exists
|
static String |
getDatabasePath(String databaseName)
Returns the file path of the Database if exists and if supported on
the platform.
|
static boolean |
isCustomPathSupported()
Checks if this platform supports custom database paths.
|
static Database |
openOrCreate(String databaseName)
Opens a database or create one if not exists.
|
abstract void |
rollbackTransaction()
Rolls back current transaction
|
static boolean |
supportsWasNull(Row row)
Checks to see if the given row supports
wasNull(com.codename1.db.Row) . |
static boolean |
wasNull(Row row)
Checks if the last value accessed from a given row was null.
|
public static boolean isCustomPathSupported()
openOrCreate(java.lang.String)
, exists(java.lang.String)
,
delete(java.lang.String)
, and getDatabasePath(java.lang.String)
.public static Database openOrCreate(String databaseName) throws IOException
databaseName
- the name of the database. Platforms that support custom database
paths (i.e. isCustomPathSupported()
return true), will also accept a file path here.IOException
- if database cannot be createdpublic static boolean exists(String databaseName)
NOTE: Not supported in the Javascript port. Will always return false.
databaseName
- the name of the database. Platforms that support custom database
paths (i.e. isCustomPathSupported()
return true), will also accept a file path here.public static void delete(String databaseName) throws IOException
NOTE: This method is not supported in the Javascript port. Will silently fail.
databaseName
- the name of the database. Platforms that support custom database
paths (i.e. isCustomPathSupported()
return true), will also accept a file path here.IOException
- if database cannot be deletedpublic static String getDatabasePath(String databaseName)
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.
public abstract void beginTransaction() throws IOException
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
IOException
- if database is not openedpublic abstract void commitTransaction() throws IOException
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
IOException
- if database is not opened or transaction was not startedpublic abstract void rollbackTransaction() throws IOException
NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.
IOException
- if database is not opened or transaction was not startedpublic abstract void close() throws IOException
IOException
public abstract void execute(String sql) throws IOException
sql
- the sql to executeIOException
public abstract void execute(String sql, String[] params) throws IOException
sql
- the sql to executeparams
- to bind to the query where the '?' existsIOException
public void execute(String sql, Object... params) throws IOException
sql
- the sql to executeparams
- to bind to the query where the '?' exists, supported object
types are String, byte[], Double, Long and nullIOException
public abstract Cursor executeQuery(String sql, String[] params) throws IOException
sql
- the sql to executeparams
- to bind to the query where the '?' existsIOException
public Cursor executeQuery(String sql, Object... params) throws IOException
sql
- the sql to executeparams
- to bind to the query where the '?' exists, supported object
types are String, byte[], Double, Long and nullIOException
public abstract Cursor executeQuery(String sql) throws IOException
sql
- the sql to executeIOException
public static boolean wasNull(Row row) throws IOException
Check supportsWasNull(com.codename1.db.Row)
to see if the platform supports
wasNull().
Currently wasNull() is supported on UWP, iOS, Android, and JavaSE (Simulator).
row
- The row to check.IOException
RowExt.wasNull()
,
supportsWasNull(com.codename1.db.Row)
public static boolean supportsWasNull(Row row) throws IOException
wasNull(com.codename1.db.Row)
.row
- The row to check.IOException
wasNull(com.codename1.db.Row)
,
RowExt.wasNull()