如何判断 codenameone 应用程序中是否存在数据库文件?
How to tell if a database file exists in a codenameone app?
我想知道我的 codenameone 应用程序中是否存在特定的数据库文件。
这是我的代码:
if (CN.existsInFileSystem(Display.getInstance()
.getDatabasePath("my.db")))
overwriteDb = false;
似乎 getDatabasePath
方法将创建数据库,如果它不存在的话,至少在 windows/eclipse 模拟器上是这样。 (我还没有在任何设备上试过)。
javadocs 说当数据库不存在时 getDatabasePath
将 return null
,但是它 return 是新创建的 db 文件的路径。
有没有办法在不实际创建零字节文件的情况下获取路径?
不,它没有创建数据库,我只是在代码中验证了它。 JavaDoc 不正确,我为下一次更新修复了它。您拥有的代码应该可以正常工作。
这是方法的Android实现:
public String getDatabasePath(String databaseName) {
if (databaseName.startsWith("file://")) {
return databaseName;
}
File db = new File(getContext().getApplicationInfo().dataDir + "/databases/" + databaseName);
return db.getAbsolutePath();
}
您会注意到 null 永远不会返回,也不会创建任何内容。固定文档如下所示:
Returns the file path of the Database if support for database exists on the platform.
@param databaseName the name of the database with out / or path elements e.g. mydatabase.db
@return the file path of the database or null if database isn't supported
我想知道我的 codenameone 应用程序中是否存在特定的数据库文件。
这是我的代码:
if (CN.existsInFileSystem(Display.getInstance()
.getDatabasePath("my.db")))
overwriteDb = false;
似乎 getDatabasePath
方法将创建数据库,如果它不存在的话,至少在 windows/eclipse 模拟器上是这样。 (我还没有在任何设备上试过)。
javadocs 说当数据库不存在时 getDatabasePath
将 return null
,但是它 return 是新创建的 db 文件的路径。
有没有办法在不实际创建零字节文件的情况下获取路径?
不,它没有创建数据库,我只是在代码中验证了它。 JavaDoc 不正确,我为下一次更新修复了它。您拥有的代码应该可以正常工作。
这是方法的Android实现:
public String getDatabasePath(String databaseName) {
if (databaseName.startsWith("file://")) {
return databaseName;
}
File db = new File(getContext().getApplicationInfo().dataDir + "/databases/" + databaseName);
return db.getAbsolutePath();
}
您会注意到 null 永远不会返回,也不会创建任何内容。固定文档如下所示:
Returns the file path of the Database if support for database exists on the platform.
@param databaseName the name of the database with out / or path elements e.g.
mydatabase.db
@return the file path of the database or null if database isn't supported