1

我正在为黑莓设备开发一个应用程序。这个应用程序包含一个数据库。由于 API 是在最后一个 API 版本上提供的,我决定使用 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();
    }
}

谢谢你的帮助,我真的不知道问题出在哪里。

4

2 回答 2

1

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).

于 2011-03-23T19:25:53.213 回答
1

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?

于 2011-03-23T21:22:24.217 回答