Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义SQLite构建-无法打开数据库_Android_Database_Sqlite_Android Ndk - Fatal编程技术网

Android自定义SQLite构建-无法打开数据库

Android自定义SQLite构建-无法打开数据库,android,database,sqlite,android-ndk,Android,Database,Sqlite,Android Ndk,我的目标是构建一个定制版本的SQLite(特别是启用了R-Tree)以包含在我的Android项目中。动机来自: SQLite提供了一些有关如何执行此操作的说明: 我已经编译了SQLite,并成功地将共享库包含在我的项目中。我遇到的问题是,只要我调用一个操作,例如getReadableDatabase()或getWriteableDatabase(),我就会收到一个SQLiteCantoPendDatabaseException 需要注意的是,我正在使用: 导入org.sqlite.data

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

SQLite提供了一些有关如何执行此操作的说明:

我已经编译了SQLite,并成功地将共享库包含在我的项目中。我遇到的问题是,只要我调用一个操作,例如
getReadableDatabase()
getWriteableDatabase()
,我就会收到一个SQLiteCantoPendDatabaseException

需要注意的是,我正在使用:

导入org.sqlite.database.sqlite.SQLiteDatabase

import org.sqlite.database.sqlite.SQLiteOpenHelper

代替:

导入android.database.sqlite.SQLiteDatabase

导入android.database.sqlite.SQLiteOpenHelper

为了通过NDK和JNI使用定制SQLite构建

MySQLiteHelper:

主要活动:

堆栈跟踪:

我在这里看到过几篇关于同一个例外的帖子,但它们似乎都与从磁盘上的其他地方导入数据库有关,因为它们没有正确设置文件路径。这似乎是最相关的帖子,但没有一条建议奏效


我用两部不同的手机和模拟器试过了,我收到了同样的错误。此外,我已经用默认的androidsqlite导入替换了上面讨论的导入,并且效果很好(因为默认的androidsqlite安装中没有包含R-Tree,所以我不尝试创建R-Tree)。将database dir和.db file的权限更改为777并没有解决此问题,将WRITE_权限添加到我的清单中也没有解决此问题。

这是SQLite Android绑定中的不兼容问题

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

db=mContext.openOrCreateDatabase(mName,menablewriteaheadloging?
Context.MODE\启用\提前写入\日志记录:0,
梅罗汉德勒工厂);
org.sqlite.database.sqlite.SQLiteOpenHelper
不使用上下文:

db=SQLiteDatabase.openOrCreateDatabase(
mName、MFFactory、mErrorHandler
);
这意味着数据库路径不在数据库名称的前面

使用类似以下内容获取数据库的完整路径:

String path=context.getDatabasePath(数据库名称).getPath();

并将其用作数据库名称。

您实际要访问的路径是什么?
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)