Android Custom SQLite Build - 无法打开数据库

时间:2014-10-30 00:02:43

标签: android database sqlite android-ndk

我的目标是构建自定义版本的SQLite(特别是启用了R-Tree)以包含在我的Android项目中。动机源于:

Android SQLite R-Tree - How to install module?

SQLite有一些如何执行此操作的说明:

http://www.sqlite.org/android/doc/trunk/www/index.wiki

我编译了SQLite并成功地在我的项目中包含了共享库。我遇到的问题是,只要我调用getReadableDatabase()getWriteableDatabase()之类的操作,我就会收到SQLiteCantOpenDatabaseException。

重要的是要注意我正在使用:

import org.sqlite.database.sqlite.SQLiteDatabase;

import org.sqlite.database.sqlite.SQLiteOpenHelper;

取代:

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

为了通过NDK和JNI使用自定义SQLite构建。

MySQLiteHelper:

public class MySQLiteHelper extends SQLiteOpenHelper {

    static {
        System.loadLibrary("sqliteX");
    }

    private static final String DATABASE_NAME = "mydata.db";
    private static final int DATABASE_VERSION = 1;
    Context myContext;

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        myContext = context;
    }

    public void getEntry(){
        SQLiteDatabase db = this.getReadableDatabase();
        db.close();
    }

    public void createEntry(){
        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        //...
        //...
        db.close();
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
         String create_rtree = "CREATE VIRTUAL TABLE demo_index USING rtree(\n" +
                "   id,              -- Integer primary key\n" +
                "   minX, maxX,      -- Minimum and maximum X coordinate\n" +
                "   minY, maxY       -- Minimum and maximum Y coordinate\n" +
                ");";

        database.execSQL(create_rtree);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + "demo_index");
        onCreate(db);
    }

}

主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MySQLiteHelper helper = new MySQLiteHelper(this);
    helper.createEntry();
    setContentView(R.layout.activity_my);
}

堆栈跟踪:

10-29 13:45:38.356    2360-2360/com.example.nathanielwendt.lstrtree E/SQLiteLog﹕ (14)   os_unix.c:30589: (2) open(//arrrr.db) -
10-29 13:45:38.376    2360-2360/com.example.nathanielwendt.lstrtree E/SQLiteDatabase﹕ Failed to open database 'arrrr.db'.
    org.sqlite.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at org.sqlite.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at org.sqlite.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:217)
        at org.sqlite.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:201)
        at     org.sqlite.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:467)
        at org.sqlite.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
        at org.sqlite.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
        at org.sqlite.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:809)
        at org.sqlite.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:794)
        at org.sqlite.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:699)
        at org.sqlite.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:722)
        at org.sqlite.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:228)
        at org.sqlite.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:168)
        at com.example.nathanielwendt.lstrtree.MySQLiteHelper.createEntry(MySQLiteHelper.java:65)
        at com.example.nathanielwendt.lstrtree.MyActivity.onCreate(MyActivity.java:25)

我在SO上看过几篇关于同样异常的帖子,但它们似乎都与导入数据库相关,而这些数据库是从磁盘上的其他位置开始的,因为它们没有正确设置文件路径。这似乎是最相关的帖子:Android SQLiteOpenHelper cannot open database file,但没有一个建议有效。

我用2个不同的手机和模拟器尝试了这个,我收到了同样的错误。此外,我已经用默认的android sqlite导入替换了上面讨论的导​​入,它工作正常(因为我没有尝试创建一个R-Tree,因为它没有包含在默认的Android SQLite安装中)。将数据库目录和.db文件的权限更改为777不能解决问题,也没有将WRITE_PERMISSION添加到我的清单。

1 个答案:

答案 0 :(得分:5)

这是SQLite Android绑定中的不兼容。

原始Android代码打开数据库,如下所示:

db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ?
        Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
        mFactory, mErrorHandler);

虽然org.sqlite.database.sqlite.SQLiteOpenHelper不使用上下文:

db = SQLiteDatabase.openOrCreateDatabase(
        mName, mFactory, mErrorHandler
);

这意味着数据库路径不会添加到数据库名称之前。

使用类似的东西来获取数据库的完整路径:

String path = context.getDatabasePath(DATABASE_NAME).getPath();

并将其用作数据库名称。