Blackberry - How to create a SQLite database?
I am developping an app for Blackberry devices. This app contains a database. As the API are provided on the last API version, I decided to use SQLite.
I followed every sample I could find everywhere but whatever happens, my database remains empty and I get a "DatabaseIOException : File system error (12)".
I should add that I am currently working on the simulator.
Here is the code related :
// Creation of the database
private static final String DATABASE_NAME = "myDatabase.db";
private static final String STORAGE_LOCATION = "/store/databases/myApp/";
try
{
// Create URI
URI uri = URI.create(STORAGE_LOCATION + DATABASE_NAME);
// Open or create a plain text database. This will create the
// directory and file defined by the URI (if they do not already exist).
db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false));
// Close the database in case it is blank and we need to write to the file
db.close();
this.CreateTable();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
Table creation method :
public void CreateTable(){
URI uri;
try {
uri = URI.create(STORAGE_LOCATION + DATABASE_NAME);
// Create a new DatabaseOptions object forcing foreign key constraints
DatabaseOptions databaseOptions = new DatabaseOptions();
databaseOptions.set( "foreign_key_constraints", "on" );
// Open开发者_开发知识库 the database
db = DatabaseFactory.open(uri, databaseOptions);
} catch (ControlledAccessException e) {
e.printStackTrace();
} catch (DatabaseIOException e) {
e.printStackTrace();
} catch (DatabasePathException e) {
e.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (MalformedURIException e1) {
e1.printStackTrace();
} catch (DatabaseOptionsSetInvalidKeyException e) {
e.printStackTrace();
} catch (DatabaseOptionsSetInvalidValueException e) {
e.printStackTrace();
}
Statement st;
try {
st = this.getDb().createStatement("CREATE TABLE '" + TABLE_NAME1 +
"'('id' PRIMARY KEY, 'libContexte' TEXT, 'libelle' TEXT, 'xcoord' REAL, "+
"'ycoord' REAL)");
st.prepare();
st.execute();
} catch (DatabaseException e) {
e.printStackTrace();
}
try {
db.close();
} catch (DatabaseIOException e) {
e.printStackTrace();
}
}
Thank you for your help, I really do not where the problem is coming from.
Just an idea - not every device supports DB creation on Device Memory (Locations of SQLite database files), so try using SDCard instead (also make sure the simulator has the SDCard).
I assume you're using an appropriate device or device simulator for storing a database on the built-in storage. That would be the 9800 or the 9550. One reason for getting an 'error 12' is that the database is already open. How are you protecting the database initialization from executing multiple times?
精彩评论