|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Database
Lets you create, modify, and access SQLite databases.
BlackBerry devices that run BlackBerry® Device Software 5.0 or later include the open source SQLite library. The Database API gives you access to this library. Although SQLite databases are optimized for use on a smartphone, the API is similar to other database APIs. The Database API is an interface for the SQLite library so that you can create, delete and execute SQLite SQL statements, as well as perform other common database management tasks. For more information about SQLite, visit http://www.sqlite.org.
Use the following approach to work with an existing SQLite database.
Database.createStatement()
.
Statement.prepare()
. Performing this step is like compiling the statement.
Statement.getCursor()
if the statement might return results and Statement.execute()
otherwise.
By default, a transaction is created for each statement. If the statement runs successfully, it is automatically committed.
If it fails, it is rolled back. If you do not want each statement to run in its own transaction (for example, if you want to
run multiple statements in one transaction, which can have performance benefits), then you should use
Database.beginTransaction()
and Database.commitTransaction()
to change the default behavior and manually manage transactions.
Nested transactions are not supported.
SQLite databases created by your application are not removed automatically when your application is removed.
import net.rim.device.api.system.Application; import net.rim.device.api.database.*; import net.rim.device.api.io.*; public class CreateDatabase extends Application { public static void main(String[] args) { CreateDatabase app = new CreateDatabase(); try { URI strURI = URI.create("file:///SDCard/test.db"); DatabaseFactory.create(strURI); } catch ( Exception e ) { System.out.println( e.getMessage() ); } } }
import net.rim.device.api.database.Database; import net.rim.device.api.database.DatabaseFactory; import net.rim.device.api.database.Statement; import net.rim.device.api.io.URI; import net.rim.device.api.system.Application; public class AddDatabaseTable extends Application { public static void main(String[] args) { AddDatabaseTable app = new AddDatabaseTable(); try { URI myURI = URI.create("/SDCard/test.db"); Database d = DatabaseFactory.open(myURI); Statement st = d.createStatement( "CREATE TABLE 'People' ( " + "'Name' TEXT, " + "'Age' INTEGER )" ); st.prepare(); st.execute(); st.close(); } catch ( Exception e ) { System.out.println( e.getMessage() ); } } }
import net.rim.device.api.database.Database; import net.rim.device.api.database.DatabaseFactory; import net.rim.device.api.database.Statement; import net.rim.device.api.io.URI; import net.rim.device.api.system.Application; public class AddDatabaseTable extends Application { public static void main(String[] args) { AddDatabaseTable app = new AddDatabaseTable(); try { URI myURI = URI.create("/SDCard/test.db"); Database d = DatabaseFactory.open(myURI); Statement st = d.createStatement("INSERT INTO People(Name,Age) " + "VALUES ('John',37)"); st.prepare(); st.execute(); st.close(); } catch ( Exception e ) { System.out.println( e.getMessage() ); } } }
Method Summary | ||
---|---|---|
|
void |
beginTransaction()
Starts a new transaction. |
|
void |
close()
Closes the database. |
|
void |
commitTransaction()
Commits the current transaction. |
|
Statement |
createStatement(String sqlStatement)
Creates a Statement in this database. |
|
DatabaseSecurityOptions |
getDatabaseSecurityOptions()
Returns a new object with populated database security options. |
|
URI |
getFile()
Returns the URI of the database file. |
|
String |
getLastError()
Returns the last error message from the database. |
|
int |
getNumberOfChanges()
Returns the number of rows affected by the last database modification. |
|
boolean |
isEncrypted()
Returns encrypted database property. |
|
long |
lastInsertedRowID()
Returns the last automatically-generated RowID (the primary key). |
|
void |
rollbackTransaction()
Rolls back the current transaction. |
Method Detail |
---|
void close() throws DatabaseIOException
Statements should be closed explicitly to free up resources.
DatabaseIOException
- If the database file can't be closed
and data can't be flushed from the file. For example, a DatabaseIOException is thrown when
close()
is called and the microSD card has been removed.Statement createStatement(String sqlStatement) throws DatabaseException
If the given SQL parameter contains multiple statements separated by semicolons (;) then only the first one is parsed and executed. The remaining statements after the first semicolon are ignored.
Statements should be closed explicitly to free up resources.
sqlStatement
- SQL statement to execute.
DatabaseException
- If the database is closed.
NullPointerException
- If the SQL statement is null.URI getFile() throws DatabasePathException
DatabasePathException
- If the specified path is incorrect. The path can be incorrect
when the microSD card is removed.int getNumberOfChanges() throws DatabaseException
DatabaseException
- If the database is closed.long lastInsertedRowID() throws DatabaseException
Each entry in a SQLite table has a unique 64-bit signed integer key called the RowID. The RowID is always available as an undeclared column named ROWID, OID, or _ROWID_ as long as those names are not also used by explicitly declared columns. If the table has a column of type INTEGER PRIMARY KEY then that column is another alias for the RowID.
Care should be taken when there are concurrent inserts to the same
database. To ensure that the correct RowID is returned, the INSERT and
the call to lastInsertedRowID()
should be done within a synchronized
block. The synchronized block should be used in a way that blocks any
other INSERT statement from executing (and modifying the last inserted
RowID).
DatabaseException
- Thrown when the database is closed.String getLastError() throws DatabaseException
DatabaseException
- If the database is closed.boolean isEncrypted() throws DatabaseException
true
if the database is encrypted; otherwise false
.
DatabaseException
- If the database is not open.DatabaseSecurityOptions getDatabaseSecurityOptions() throws DatabaseException
DatabaseException
- If the database is not open.void beginTransaction() throws DatabaseException
DatabaseException
is thrown.
DatabaseException
- If the database is not open or if this method is called twice.void commitTransaction() throws DatabaseException
If the application does not close all cursors that were started within the transaction, the commit fails.
DatabaseException
- If the database is not open or the transaction was not started.void rollbackTransaction() throws DatabaseException
DatabaseException
- If the database is not open or the transaction was not started.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1999-2010 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark of Sun Microsystems, Inc.