gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:28:38 25 4
gpt4 key购买 nike

我正在尝试将 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

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

注意:我用我的 Spatialite 和我从他们网站下载的 mod_spatialite 测试了这个。

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

最佳答案

基于示例here我在 sqlite 中启用了 spatialite,该函数启用了该模块。为此,您必须链接 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;
}

例子:

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();
}

输出:

**** 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 "

可以找到完整的例子here .

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

关于c++ - 无法在 qSqlite ( QT 5.9) 中加载 Spatialite 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com