我正在開發黑莓設備的應用程序。這個程序包含一個數據庫。由於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