2011-03-23 114 views
1

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

謝謝您的幫助,我真的不知道哪裏出了問題的來源。

+0

可以在這裏找到一步一步的解決方案http://developer.blackberry.com/bbos/java/documentation/sqlite_creating_1981756_11.html – 2014-01-09 09:11:46

回答

1

只是一個想法 - 並非每個設備都支持在設備內存上創建數據庫(Locations of SQLite database files),所以請嘗試使用SDCard(也請確保模擬器具有SDCard)。

+1

eMMC要求通常會導致「filesystem not ready」異常,而不是「錯誤12' – 2011-03-23 21:20:13

1

我假設您正在使用適當的設備或設備模擬器來在內置存儲上存儲數據庫。那將是9800或9550.獲得'錯誤12'的一個原因是數據庫已經打開。你如何保護數據庫初始化多次執行?