无法在 qSqlite 中加载 Spatialite 扩展(QT 5.9)

Mhd*_*fia 3 c++ sqlite qt spatialite qt5

我正在尝试将 Spatialite 作为 qSqlite (Qt 5.9)中的扩展加载,我之前已经使用 Qt4.8 完成了此操作,但使用 QT5.9 失败了。我通过删除“SQLITE_OMIT_LOAD_EXTENSION”更改了 sqlite.pri,并通过删除“ #define SQLITE_OMIT_LOAD_EXTENSION 1”并添加“ #define SQLITE_ENABLE_LOAD_EXTENSION 1”对 sqlite.c 进行了一些更改。我还将以下行添加到 openDatabase(....)

#if defined(SQLITE_ENABLE_LOAD_EXTENSION)
| SQLITE_LoadExtension|SQLITE_LoadExtFunc
#endif
Run Code Online (Sandbox Code Playgroud)

现在“requet.setQuery(“SELECT load_extension('spatialite')”, dbProject);” 函数已被识别,但我收到此消息:错误“找不到指定的过程。\r\n无法获取行”如果我查看 MSVC14 中的调试输出,我可以看到 Spatialite.dll 和所有它的依赖项已被加载。

注意:我用我的 Spatialite 以及从他们的网站下载的 mod_spatialite 对此进行了测试。

关于这个问题有什么想法吗?提前致谢。

eyl*_*esc 5

根据这里的示例,我在 sqlite 中启用了 spaceite,该函数启用了该模块。为此,您必须链接 sqlite3 库。

所做的修改是:

  • 改成"SELECT load_extension('libspatialite.so')""SELECT load_extension('mod_spatialite')"

  • 改成"SELECT InitSpatialMetadata()""SELECT InitSpatialMetadata(1)"


#include <sqlite3.h>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlError>

int enable_spatialite(QSqlDatabase db){
    QVariant v = db.driver()->handle();
    if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
    {
        sqlite3_initialize();
        sqlite3 *db_handle = *static_cast<sqlite3 **>(v.data());

        if (db_handle != 0) {
            sqlite3_enable_load_extension(db_handle, 1);

            QSqlQuery query;

            query.exec("SELECT load_extension('mod_spatialite')");
            if (query.lastError() .isValid())
            {
                qDebug() << "Error: cannot load the Spatialite extension (" << query.lastError().text()<<")";
                return 0;
            }

            qDebug()<<"**** SpatiaLite loaded as an extension ***";

            query.exec("SELECT InitSpatialMetadata(1)");
            if (query.lastError() .isValid())
            {
                qDebug() << "Error: cannot load the Spatialite extension (" << query.lastError().text()<<")";
                return 0;
            }
            qDebug()<<"**** InitSpatialMetadata successful ***";

            return 1;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

例子:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db =  QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName("memory.db");
    if (!db.open()) {
        qDebug()<<"not open";
    }

    qDebug()<<enable_spatialite(db);

    QSqlQuery query;

    qDebug()<<query.exec("CREATE TABLE test_geom (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, measured_value DOUBLE NOT NULL);");
    qDebug()<<query.exec("SELECT AddGeometryColumn('test_geom', 'the_geom', 4326, 'POINT', 'XY');");



    for(int i=0; i< 10; i++){
        QString q = QString("INSERT INTO test_geom(id, name, measured_value, the_geom) VALUES (%1,'point %2', %3, GeomFromText('POINT(1.01 2.02)', 4326))")
                .arg("NULL").arg(i).arg(i);
        query.prepare(q);
        qDebug()<< i<<query.exec();
    }

    qDebug()<<query.exec("SELECT id, name, measured_value,  AsText(the_geom), ST_GeometryType(the_geom), ST_Srid(the_geom) FROM test_geom");


    while (query.next()) {
        QString str;
        for(int i=0; i < query.record().count(); i++)
            str += query.value(i).toString() + " ";
        qDebug()<<str;
    }
    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

输出:

**** SpatiaLite loaded as an extension ***
**** InitSpatialMetadata successful ***
1
true
true
0 true
1 true
2 true
3 true
4 true
5 true
6 true
7 true
8 true
9 true
true
"1 point 0 0 POINT(1.01 2.02) POINT 4326 "
"2 point 1 1 POINT(1.01 2.02) POINT 4326 "
"3 point 2 2 POINT(1.01 2.02) POINT 4326 "
"4 point 3 3 POINT(1.01 2.02) POINT 4326 "
"5 point 4 4 POINT(1.01 2.02) POINT 4326 "
"6 point 5 5 POINT(1.01 2.02) POINT 4326 "
"7 point 6 6 POINT(1.01 2.02) POINT 4326 "
"8 point 7 7 POINT(1.01 2.02) POINT 4326 "
"9 point 8 8 POINT(1.01 2.02) POINT 4326 "
"10 point 9 9 POINT(1.01 2.02) POINT 4326 "
Run Code Online (Sandbox Code Playgroud)

完整的示例可以在这里找到。

此代码已在linux Arch Linux 4.11.3-1-ARCH、Qt 5.8上测试