我正在开发黑莓设备的应用程序。这个程序包含一个数据库。由于API是在最新的API版本上提供的,因此我决定使用SQLite。Blackberry - 如何创建SQLite数据库?
我跟踪了每个可以在任何地方找到的示例,但无论发生什么情况,我的数据库仍为空,并且出现“DatabaseIOException:文件系统错误(12)”。
我应该补充一点,我目前正在模拟器上工作。
下面是相关的代码:
// 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();
}
表的创建方法:
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();
}
}
谢谢您的帮助,我真的不知道哪里出了问题的来源。
可以在这里找到一步一步的解决方案http://developer.blackberry.com/bbos/java/documentation/sqlite_creating_1981756_11.html – 2014-01-09 09:11:46