2011-03-23 115 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'的一个原因是数据库已经打开。你如何保护数据库初始化多次执行?