Blackberry - 如何创建 SQLite 数据库?
我正在为黑莓设备开发一个应用程序。这个应用程序包含一个数据库。由于 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();
}
}
谢谢你的帮助,我实在不知道问题出在哪里。
I am developping an app for Blackberry devices. This app contains a database. As the API are provided on the last API version, I decided to use SQLite.
I followed every sample I could find everywhere but whatever happens, my database remains empty and I get a "DatabaseIOException : File system error (12)".
I should add that I am currently working on the simulator.
Here is the code related :
// 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();
}
Table creation method :
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();
}
}
Thank you for your help, I really do not where the problem is coming from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是一个想法 - 并非每个设备都支持在设备内存上创建数据库(SQLite 的位置数据库文件),所以尝试使用SDCard(还要确保模拟器有SDCard)。
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).
我假设您正在使用适当的设备或设备模拟器在内置存储上存储数据库。那将是 9800 或 9550。收到“错误 12”的原因之一是数据库已打开。如何防止数据库初始化多次执行?
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?