1

当我尝试将新文件添加到我的数据库时,我在 Spatialite 中收到以下错误。

10-05 11:50:01.408: E/GDH(1545): jsqlite.Exception: unable to open database file
10-05 11:50:01.408: E/GDH(1545):    at jsqlite.Database._exec(Native Method)
10-05 11:50:01.408: E/GDH(1545):    at jsqlite.Database.exec(Database.java:177)
10-05 11:50:01.408: E/GDH(1545):    at com.kd7uiy.hamfinder.database.GeoDataHelper.addDb(GeoDataHelper.java:153)

此代码在与 UI 线程不同的线程中运行,并执行以下代码,第 153 行标记自 GeoDataHelper.addDb。

Database db = this.getWritableDatabase();
TableResult result=new TableResult(); //Filler, isn't really needed, but...
db.exec("ATTACH DATABASE \"%q\" AS newDb",result,new String[]{path});
db.exec("REPLACE INTO counties(name,cntyidfp,geometry) "
        + "SELECT name,cntyidfp,geometry FROM newDb.counties",result);
db.exec("VACUUM",result);
db.exec("DETACH DATABASE newDb",result);

打开数据库的代码包括:

Database db = new Database();
db.open(mDatabasePath + "/" + mName,
        jsqlite.Constants.SQLITE_OPEN_READWRITE);
// set parameters
db.exec("PRAGMA journal_mode = PERSIST;", null);
db.exec("PRAGMA temp_store = FILE;", null);
db.exec("PRAGMA temp_store_directory = \"" + mDatabasePath+"/"+mName + "\";", null);

我不明白前两个调用如何有效,但 VACUUM 不是。有任何想法吗?

4

1 回答 1

1

当我在写这个答案时,我意识到一件重要的事情。VACUUM取决于是否有可用的临时目录。我从网上找到的代码使用了一个临时目录,但不是正确的,即缓存目录。将代码更改为此解决了问题:

Database db = new Database();
db.open(mDatabasePath + "/" + mName,
        jsqlite.Constants.SQLITE_OPEN_READWRITE);
// set parameters
db.exec("PRAGMA journal_mode = PERSIST;", null);
db.exec("PRAGMA temp_store = FILE;", null);
db.get_table("PRAGMA temp_store_directory = \"%q\";", new String[]{mContext.getCacheDir().toString()});
于 2014-10-05T12:07:24.937 回答